Random Number Generator not very random?



  • I think there's a bit of an issue about trusting randomness to come out of the RNG. Here's a quick sample from my LoPy with 0.9.0.b2:

    >>> import machine
    >>> randomnumbers=[machine.rng() for i in range(40)]
    >>> randomnumbers
    [14745600, 7372800, 3686400, 1843200, 921600, 460800, 230400, 115200, 57600, 28800, 14400, 7200, 3600, 1800, 900, 450, 225, 14745712, 7372856, 3686428, 1843214, 921607, 15075331, 9569281, 11010560, 5505280, 2752640, 1376320, 688160, 344080, 172040, 86020, 43010, 21505, 14756352, 7378176, 3689088, 1844544, 922272, 461136]
    >>> [r/r2 for r,r2 in zip(randomnumbers,randomnumbers[1:])]
    [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.525867e-05, 2.0, 2.0, 2.0, 2.0, 0.06113344, 1.575388, 0.8691001, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.001457338, 2.0, 2.0, 2.0, 2.0, 2.0]
    >>> 
    

    So at what rate can we expect to see anything unpredictable out of it?



  • Looks like an LFSR clocked by the calls themselves, something like n=n>>1 ^ (0xe10000 if n&1 else 0). It might have been seeded with 1. These things are half decent for unimportant things if you extract a single bit (not the entire state) and they're scrambled, but we'd like to have a true hardware RNG too (the ESP32 should have it).



  • Hi @LoneTech thanks for reporting. We going to fix it at next firmware release.


Log in to reply
 

Pycom on Twitter