Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Random Number Generator not very random?

    Scheduled Pinned Locked Moved MicroPython
    3 Posts 2 Posters 1.5k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L Offline
      LoneTech
      last edited by LoneTech

      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?

      1 Reply Last reply Reply Quote 0
      • ? Offline
        A Former User
        last edited by

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

        1 Reply Last reply Reply Quote 0
        • L Offline
          LoneTech
          last edited by LoneTech

          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).

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom