mercredi 8 juillet 2015

How can I generate the same random number in JavaScript and Python?

In Python, if I use this code:

import random
random.seed(123)
print(random.random())

The first random number will always be 0.052363598850944326, since I'm giving it the same seed every time I run the program.

JavaScript doesn't have a built-in way to use seeds. I've tried out a JavaScript script called mersenne-twister.js, since that's the type of PRNG that Python uses. But even if I use the same seed, I get a different result than I do in Python.

var m = new MersenneTwister(123);
m.genrand_int32();

That code will always return 0.6964691872708499 the first time that it's run.

How can I use a given seed to get the same random number in both Python and JavaScript?




Aucun commentaire:

Enregistrer un commentaire