Skip to content

Commit 451243c

Browse files
blazonceksofthack007
authored andcommitted
Merge pull request wled#3552 from TripleWhy/seedRandom16
Seed FastLED's RNG
1 parent 68dc4d1 commit 451243c

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

wled00/FX.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,7 @@ uint16_t mode_multi_comet(void) {
17221722
}
17231723
comets[i]++;
17241724
} else {
1725-
if(!random(SEGLEN)) {
1725+
if(!random16(SEGLEN)) {
17261726
comets[i] = 0;
17271727
}
17281728
}
@@ -2074,7 +2074,7 @@ uint16_t mode_fire_2012() {
20742074

20752075
// Step 1. Cool down every cell a little
20762076
for (int i = 0; i < SEGLEN; i++) {
2077-
uint8_t cool = (it != SEGENV.step) ? random8((((20 + SEGMENT.speed/3) * 16) / SEGLEN)+2) : random(4);
2077+
uint8_t cool = (it != SEGENV.step) ? random8((((20 + SEGMENT.speed/3) * 16) / SEGLEN)+2) : random8(4);
20782078
uint8_t minTemp = (i<ignition) ? (ignition-i)/4 + 16 : 0; // should not become black in ignition area
20792079
uint8_t temp = qsub8(heat[i], cool);
20802080
heat[i] = temp<minTemp ? minTemp : temp;
@@ -4580,15 +4580,15 @@ class AuroraWave {
45804580

45814581
public:
45824582
void init(uint32_t segment_length, CRGB color) {
4583-
ttl = random(500, 1501);
4583+
ttl = random16(500, 1501);
45844584
basecolor = color;
4585-
basealpha = random(60, 101) / (float)100;
4585+
basealpha = random8(60, 101) / (float)100;
45864586
age = 0;
4587-
width = random(segment_length / 20, segment_length / W_WIDTH_FACTOR); //half of width to make math easier
4587+
width = random16(segment_length / 20, segment_length / W_WIDTH_FACTOR); //half of width to make math easier
45884588
if (!width) width = 1;
4589-
center = random(101) / (float)100 * segment_length;
4590-
goingleft = random(0, 2) == 0;
4591-
speed_factor = (random(10, 31) / (float)100 * W_MAX_SPEED / 255);
4589+
center = random8(101) / (float)100 * segment_length;
4590+
goingleft = random8(0, 2) == 0;
4591+
speed_factor = (random8(10, 31) / (float)100 * W_MAX_SPEED / 255);
45924592
alive = true;
45934593
}
45944594

@@ -4673,7 +4673,7 @@ uint16_t mode_aurora(void) {
46734673
waves = reinterpret_cast<AuroraWave*>(SEGENV.data);
46744674

46754675
for (int i = 0; i < SEGENV.aux1; i++) {
4676-
waves[i].init(SEGLEN, CRGB(SEGMENT.color_from_palette(random8(), false, false, random(0, 3))));
4676+
waves[i].init(SEGLEN, CRGB(SEGMENT.color_from_palette(random8(), false, false, random8(0, 3))));
46774677
}
46784678
} else {
46794679
waves = reinterpret_cast<AuroraWave*>(SEGENV.data);
@@ -4685,7 +4685,7 @@ uint16_t mode_aurora(void) {
46854685

46864686
if(!(waves[i].stillAlive())) {
46874687
//If a wave dies, reinitialize it starts over.
4688-
waves[i].init(SEGLEN, CRGB(SEGMENT.color_from_palette(random8(), false, false, random(0, 3))));
4688+
waves[i].init(SEGLEN, CRGB(SEGMENT.color_from_palette(random8(), false, false, random8(0, 3))));
46894689
}
46904690
}
46914691

@@ -5993,7 +5993,7 @@ uint16_t mode_2Dghostrider(void) {
59935993
if (lighter->reg[i]) {
59945994
lighter->lightersPosY[i] = lighter->gPosY;
59955995
lighter->lightersPosX[i] = lighter->gPosX;
5996-
lighter->Angle[i] = lighter->gAngle + random(-10, 10);
5996+
lighter->Angle[i] = lighter->gAngle + ((int)random8(20) - 10);
59975997
lighter->time[i] = 0;
59985998
lighter->reg[i] = false;
59995999
} else {
@@ -6928,7 +6928,7 @@ uint16_t mode_puddlepeak(void) { // Puddlepeak. By Andrew Tuline.
69286928

69296929
uint16_t size = 0;
69306930
uint8_t fadeVal = map(SEGMENT.speed,0,255, 224, 254);
6931-
uint16_t pos = random(SEGLEN); // Set a random starting position.
6931+
uint16_t pos = random16(SEGLEN); // Set a random starting position.
69326932

69336933
um_data_t *um_data;
69346934
if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) {

wled00/wled.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,20 @@ void WLED::setup()
735735
DEBUG_PRINT(pcTaskGetTaskName(NULL)); DEBUG_PRINT(F(" free stack ")); DEBUG_PRINTLN(uxTaskGetStackHighWaterMark(NULL));
736736
#endif
737737

738+
// Seed FastLED random functions with an esp random value, which already works properly at this point.
739+
#if defined(ARDUINO_ARCH_ESP32)
740+
uint32_t seed32 = esp_random();
741+
seed32 ^= random(); // WLEDMM some extra entropy (for older frameworks where esp_ramdom alone might be too predictable after startup)
742+
#elif defined(ARDUINO_ARCH_ESP8266)
743+
const uint32_t seed32 = RANDOM_REG32;
744+
#else
745+
const uint32_t seed32 = random(std::numeric_limits<long>::max());
746+
#endif
747+
random16_set_seed((uint16_t)((seed32 & 0xFFFF) ^ (seed32 >> 16)));
748+
749+
#if WLED_WATCHDOG_TIMEOUT > 0
738750
enableWatchdog();
751+
#endif
739752

740753
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_DISABLE_BROWNOUT_DET)
741754
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); //enable brownout detector

0 commit comments

Comments
 (0)