--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/a6d3f8ce-81ca-40c4-a60e-209e70941fdan%40googlegroups.com.
If a new seeding method is added, then a backward compatible seeder will be offered.
The generator’s random() method will continue to produce the same sequence when the compatible seeder is given the same seed."
Definetely not a bug.
It is ok to rely on replicability over different runs on the same interprteer, same OS,but I doubt it is repeatable even across Python versions.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/a6d3f8ce-81ca-40c4-a60e-209e70941fdan%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/059a6b99-7847-4915-8718-3e89c9f89b1fn%40googlegroups.com.
cpython : random.seed (0) ; random.getstate ()random.js : r = RandomStream () ; r.init_by_array ([0], 1) ; r.getstate ()
random.getstate = function(){return [VERSION, mt, mti]}
Here's what I learned so far about random.seed in brython vs cpython. I've done all I can. Any further work is up to Pierre if he wants to make them compatible.1. cpython uses a c module to implement mersenne twister algorithm. source code2. brython uses random.js to implement mersenne twister. source codethe js code is derived from same c source. all parameters appear to be the same.3. Calling random.seed in cpython seems to call _random_Random_seed_impl in the c module, which then calls random_seed. No matter the seed type (int, string, bytes, etc), random_seed calls init_by_array to setup the mersenne twister state.4. brython's random.js also has a function init_by_array that seems to work the same. However this function is never called anywhere that I can see. Instead, calling random.seed uses init_genrand to setup the mersenne twister state. This initializes state in a different way than the cpython code. So that's why results are different in brython.5. I ran a slightly modified random.js locally to see if init_by_array works the same as in the C module. It does. These two sequences return the same internal state:cpython : random.seed (0) ; random.getstate ()random.js : r = RandomStream () ; r.init_by_array ([0], 1) ; r.getstate ()This means brython random module could generate the same sequence as cpython from a given seed. All that's lacking is using the same calls to setup the RNG.That said, fixing it would be a bit tricky. Python random.seed accepts ints, strings, or bytes. Cpython does setup in random_seed before calling init_by_array. Each type is handled somewhat differently. Brython would have to replicate that handling in random.js then call init_by_array to setup the RNG state. It's possible, just requires translating this c code to js. The c function is not self-contained, it uses many functions and macros defined elsewhere.One more thing - I noticed that brython and cpython return slightly different objects from random.getstate(). In both cases, the state object has 3 elements:
- a version number, which is always 3
- an array of unsigned ints (32-bit) representing the mersenne twister state
- the length of the array, which is always 624
Brython returns a tuple as : (version, int array, len)Cpython returns a tuple as : (version, int array + [624], none). see source code.In other words, Cpython puts the array length as an item at the end of the int array, and the third return value is empty. Not sure why, perhaps it's a bug, but the format isn't documented anywhere. Anyway it's been like that since at least python 2.7, so it's unlikely to change.If brython wants to return compatible RNG state, then getstate in random.js need to be updated, particularly line 294:random.getstate = function(){return [VERSION, mt, mti]}where the second element should be mti appended to mt, and the third element is empty. And setstate needs to be similarly updated to read the values in using that format.With this change, you could save a PRNG state in cpython with random.getstate and restore it in brython with random.setstate (or vice versa). This would let the same pseudo-random sequence be generated across platforms, or a sequence started on one platform and resumed on another, without using a seed value. Something to think about.HTH,