-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Seed FastLED's RNG #3552
Seed FastLED's RNG #3552
Conversation
This will inevitably change the way some effects work and may break certain presets or playlists. |
This allows effects or color palettes that use FastLED's RNG to be different each time the device is powered on.
6a8b5d4
to
25c5e82
Compare
Rebased. It does indeed change some effects, if they are configured as boot preset. But that's the point. I would like effects involving randomness to be different each time. The PR does not affect playlist shuffles as they don't use FastLED's random function, but Arduino's instead. I don't know of other ways how this could affect playlists. |
@softhack007 if you have no objections I propose we merge. |
Hi, thanks for the PR, generally looks good to me. Maybe let's spend a few thoughts about point 2 below.
Line 35 in 1dab26b
|
@@ -495,6 +495,16 @@ void WLED::setup() | |||
initServer(); | |||
DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap()); | |||
|
|||
// Seed FastLED random functions with an esp random value, which already works properly at this point. | |||
#if defined(ARDUINO_ARCH_ESP32) | |||
const uint32_t seed32 = esp_random(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: please check if the header file for esp_random is already included.
#if defined(ARDUINO_ARCH_ESP32)
#include "esp_random.h"
#endif
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I also include something for the esp8266?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure ... is there a header on 8266 that defines RANDOM_REG32
?
I'd like to distinguish 2 aspects of
For effects I'd like the second but for everything else the first should be chosen IMO. FastLED uses simple prime multiplication/addition requiring seed to be truly randomised. If you want to avoid misconfigured bootloader then randomize seed by reading unconnected analog input on boot. What are the chances of misconfigured bootloader? IDK There's also this. |
@softhack007 replacing random with random8/16 👍 |
@blazoncek good point 👍 Actually - on esp32 - random() is implemented using esp_random(), so its a fair/good quality random number already. And its not based on standard libc "rand()" which has low quality. I only found references to
EDIT: in the latest framework, calling randomSeed() switches back to "soft random" with low quality 🤦 . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK for me. It seems that "seeding" the standard arduino random() would be a bad move on esp32, so using fastLED random8/16 in effects, and seeding it once from esp_random(), is best.
const uint32_t seed32 = esp_random(); | ||
#elif defined(ARDUINO_ARCH_ESP8266) | ||
const uint32_t seed32 = RANDOM_REG32; | ||
#else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this "else" branch needed? We only support esp32 variants (ARDUINO_ARCH_ESP32) and 8266 (ARDUINO_ARCH_ESP8266).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but it doesn't hurt, does it? If someone were to come along and try to port WLED to a different architecture, this would be one less thing to worry about. I don't have strong feelings about this though, and can remove it if you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if you remove it - its unused code and we don't have any plans for supporting non-esp architectures.
It crossed my mind that not everyone may want truly random seed at start. Some may prefer a known seed and known or predictable @TripleWhy please comment on the discussion above. |
I don't really have strong opinion on which random function is better. On the ESP8266 it looks to me like arduino's unseeded(!) random is faster, more random, and slightly biased, but on the ESP32 it contains a (theoretically) unlimited loop. (At least, in the version that VS Code shows me. The link that softhack007 posted does not contain loops.)
I saw that too, but I assumed initializing those systems would be part of WLED somewhere, so I just checked if esp_random returns different values on my device, at the point where I added it. I does that, so I assumed it would be fine to use it. Even if the output isn't truly random at that point, it only really matters that the output is different each boot.
It uses esp_random() without additional checks.
Feel free to create a better seed, I just wanted to propose seeding and one way to do it, instead of just submitting a feature request.
Same for ESP8266.
Someone who wants predictable results probably wouldn't choose an effect containing randomness in the first place... |
@TripleWhy please don't feel offended or provoked. I just wanted feedback and your thoughts. As mentioned above I'm ok with PR as-is. If there is a request to produce less random I am going to merge as-is. P.S. @softhack007 the only other |
@blazoncek thanks for checking - considering the "lower quality" of random8/16 functions, I think its best to only use them in effects. |
I wasn't. Sorry if I made it sound that way.
Well. Seems like I'm not able to address the code comments after all ^^ |
Seed FastLED's RNG
Seed FastLED's RNG
This allows effects or color palettes that use FastLED's RNG to be different each time the device is powered on.
I also made sure that all effects use use FastLED's random functions instead of arduino's for more consistency.