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

    Seeding random number generator

    Scheduled Pinned Locked Moved Comments & Feedback
    6 Posts 2 Posters 52 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.
    • V Offline
      Valimir Cool
      last edited by

      Hello, I'm looking for a way to seed the pseudo number generator (machine.rng() or uos.urandom()). There must be a way to do this but I couldn't figure it out how.

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @Valimir Cool
        last edited by

        @Valimir-Cool There is no API provided for seeding. So you have to implement your own RNG. The internal pseudo-rng is seeded by a combination of the unique chip ID and the actual time.

        V 1 Reply Last reply Reply Quote 0
        • V Offline
          Valimir Cool
          last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • V Offline
            Valimir Cool @robert-hh
            last edited by

            @robert-hh Thanks- It's quite disapointing having a pseudo rng without the possibillity of manually setting the seed... And I can't really see any reason for not giving this possibility that IMO is quite important.

            robert-hhR 1 Reply Last reply Reply Quote 0
            • robert-hhR Offline
              robert-hh @Valimir Cool
              last edited by

              @Valimir-Cool The urandom module of the main MP version is missing. Below you have a Python port of the MicroPython PRNG with just two of the functions. More can easily added:

              # Yasmarang random number generator
              # by Ilya Levin
              # http://www.literatecode.com/yasmarang
              # Public Domain
              
              class PRNG:
                  def __init__(self):
                      self.seed()
              
                  def yasmarang(self):
                      self.yasmarang_pad = (self.yasmarang_pad + self.yasmarang_dat + self.yasmarang_d * self.yasmarang_n) & 0xffffffff
                      self.yasmarang_pad = ((self.yasmarang_pad << 3) + (self.yasmarang_pad >> 29)) & 0xffffffff
                      self.yasmarang_n = self.yasmarang_pad | 2
                      self.yasmarang_d ^= ((self.yasmarang_pad << 31) + (self.yasmarang_pad >> 1)) & 0xffffffff
                      self.yasmarang_dat ^= (self.yasmarang_pad ^ (self.yasmarang_d >> 8) ^ 1) & 0xff
                      return (self.yasmarang_pad ^ (self.yasmarang_d << 5) ^ (self.yasmarang_pad >> 18) ^ (self.yasmarang_dat << 1)) & 0xffffffff
              
                  def get_int(self):
                      return self.yasmarang()
              
                  def seed(self, seed = 0xeda4baba):
                      self.yasmarang_pad = seed
                      self.yasmarang_n = 69
                      self.yasmarang_d = 233
                      self.yasmarang_dat = 0
              
              V 1 Reply Last reply Reply Quote 2
              • V Offline
                Valimir Cool @robert-hh
                last edited by

                @robert-hh Nice! Thank you Robert!

                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