[SOLVED] Seeding JS Math.random() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[SOLVED] Seeding JS Math.random()

Is there a simple way to seed the JS Math.random() so that it will return the same number each time just like Python’s random.seed() works?

24th Aug 2018, 7:21 PM
Nathan Lewis
Nathan Lewis - avatar
6 Answers
+ 7
no such option with the Math.random() function but try looking here for alternative https://stackoverflow.com/questions/424292/seedable-javascript-random-number-generator
24th Aug 2018, 7:24 PM
Burey
Burey - avatar
+ 2
Nathan Lewis I've been using the library suggested in one of my codes: https://code.sololearn.com/W01q2RTV4aXB/?ref=app
24th Aug 2018, 7:40 PM
🤖 Basile Laderchi
+ 2
Nathan Lewis exactly like here: https://github.com/davidbau/seedrandom/blob/released/README.md I've included it from cdnjs, initialized the generator with passing to Math.seedrandom an array of numbers and then using Math.random to get my numbers.
24th Aug 2018, 10:39 PM
🤖 Basile Laderchi
+ 2
🤖 Basile Laderchi perfect, I messed up on the script placement. works perfectly! thanks so much(:
24th Aug 2018, 10:42 PM
Nathan Lewis
Nathan Lewis - avatar
0
thanks everyone! 🤖 Basile Laderchi i was having trouble with seedrandom, could you explain how it works? edit: Nevermind i put the script in the wrong place😂😂
24th Aug 2018, 8:22 PM
Nathan Lewis
Nathan Lewis - avatar
0
Nathan Lewis you're welcome
24th Aug 2018, 10:49 PM
🤖 Basile Laderchi