Random numbers #5259
Replies: 1 comment
-
Posted at 2016-01-31 by tve I haven't done anything special with random number stuff for the esp8266 port. I'm not seeing anything in the SDK to get a random number, so I think you have to seed the software random number generator with something clever... Posted at 2016-01-31 by AlexOwen E.srand(v) should seed it, but for some reason isn't. Looking at the implementation of it it seems to have an if statement for STM chips which gets a random number from an analog reading, but (I think) on other chips just gets a random number from the generator (which is always the same). Posted at 2016-02-01 by @gfwilliams Yes, The STM32 (in jshardware.c) tries to seed the random number generator on startup using noise from the bottom bit of the ADC while looking at (iirc) the temperature of the chip. Posted at 2016-02-02 by tve jshGetRandomNumber listerally returns Posted at 2016-02-02 by @gfwilliams Ahh, right. Posted at 2016-02-03 by tve yup, the ADC would probably work, even if it's measuring a floating pin... Posted at 2016-02-03 by AlexOwen @tve is the ADC currently accessible? Does it need a considerable amount of work to get it up and running? Thanks! Posted at 2016-02-03 by @MaBecker use
Posted at 2016-02-03 by AlexOwen Unfortunately any call to analogRead seems to return 1, regardless of the pin. It doesn't even currently check whether a pin is valid, analogRead(100) and analogRead() also return 1. Posted at 2016-02-03 by @MaBecker on ESP8266 12E it is attached to the ADC pin, no input no value. Posted at 2016-02-03 by AlexOwen Ah, that's the problem there. I hooked it up and managed to get a reading out of 1024, but without something to create noise it's always going to be a fixed value. Posted at 2016-02-03 by AlexOwen I've found that this bit of code is just about good enough for me for the time being:
It gives a pseudo-random number due to the granularity of the timer. Posted at 2016-02-03 by @MaBecker works fine for me, if you like, use the short way to convert r to integer ;-)
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-30 by AlexOwen
Is there currently any way to get a random number from the ESP8266? I currently have a bit of code that needs to generate a random ID on boot, but each time it gets the same ID.
I've tried seeding the software random (which seems to do nothing) and the hardware random number generator (which also gives the same result each time).
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions