Every time the device restarts, the random number is a fixed value

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
lgyer
Posts: 24
Joined: Fri Feb 26, 2021 8:09 am

Every time the device restarts, the random number is a fixed value

Post by lgyer » Tue Apr 27, 2021 1:12 pm

Hi guys

When I use random api to get a random number at hardware rebooting, the value is a fixed number.
Even if I use the time.ticks_ms() to set the different random seed, the value is still a fixed number.

Code: Select all

import random
impot time
seed = time.ticks_ms() % 7
random.seed(seed)
print("{}".random.randint(1,100))
Can someone tell me why is that?

User avatar
Roberthh
Posts: 2682
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Every time the device restarts, the random number is a fixed value

Post by Roberthh » Tue Apr 27, 2021 2:40 pm

There was a change recently to seed the rng with more random value on boot. Which board and firmware version do you use?

lgyer
Posts: 24
Joined: Fri Feb 26, 2021 8:09 am

Re: Every time the device restarts, the random number is a fixed value

Post by lgyer » Wed Apr 28, 2021 2:02 am

Roberthh wrote:
Tue Apr 27, 2021 2:40 pm
There was a change recently to seed the rng with more random value on boot. Which board and firmware version do you use?
Hi Roberthh

My board is ESP32 and firmware version is v1.12. Could you tell me how to fix this problem? Or has the change been merged into v1.15?

User avatar
Roberthh
Posts: 2682
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Every time the device restarts, the random number is a fixed value

Post by Roberthh » Wed Apr 28, 2021 5:42 am

Just use 1.15. The change was merged in v1.14.

lgyer
Posts: 24
Joined: Fri Feb 26, 2021 8:09 am

Re: Every time the device restarts, the random number is a fixed value

Post by lgyer » Wed Apr 28, 2021 6:22 am

Roberthh wrote:
Wed Apr 28, 2021 5:42 am
Just use 1.15. The change was merged in v1.14.
Hi Roberthh

I has tried to copy the extmod/modurandom.c of v1.15 into my project, but the problem is still existing....
Is there any point I should take care?

By the way, I'm not using ctrl+D to reboot, I just turn on/off the power directly. And it's interesting that even if I execute the command "random.randint(1,12)" twice continuely, the number which I get still is same at every boot time.

Thanks

User avatar
Roberthh
Posts: 2682
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Every time the device restarts, the random number is a fixed value

Post by Roberthh » Wed Apr 28, 2021 11:25 am

It is a definition in mpconfigport.h which has to be changed. The new value is:

#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (esp_random())

User avatar
pythoncoder
Posts: 5193
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Every time the device restarts, the random number is a fixed value

Post by pythoncoder » Wed Apr 28, 2021 12:36 pm

In my testing on ESP32 with current firmware, uos.urandom() is not deterministic after a power cycle.
Peter Hinch

lgyer
Posts: 24
Joined: Fri Feb 26, 2021 8:09 am

Re: Every time the device restarts, the random number is a fixed value

Post by lgyer » Thu Apr 29, 2021 2:43 am

Hi Roberthh

I really appreciate your help.
After I add that macro, it can work.

Thanks

scruss
Posts: 77
Joined: Sat Aug 12, 2017 2:27 pm

Re: Every time the device restarts, the random number is a fixed value

Post by scruss » Fri Apr 30, 2021 3:59 pm

just out of interest, where does the source for the random module live? The only source I can find is micropython-lib/random/random.py, which is incomplete and has no code for initializing/seeding.

User avatar
Roberthh
Posts: 2682
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Every time the device restarts, the random number is a fixed value

Post by Roberthh » Fri Apr 30, 2021 4:09 pm

It is a built-in module. The source code is here: https://github.com/micropython/micropyt ... durandom.c

Post Reply