Seeding random number generator
-
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.
-
@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.
-
This post is deleted! -
@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.
-
@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 -
@robert-hh Nice! Thank you Robert!
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