// Button Entropy Injection for random() and randomSeed()
//https://wokwi.com/projects/421713344617482241
// for https://forum.arduino.cc/t/int-rndseed-analogread-a0-analogread-a0/1349109
const int ButtonPin = 2;
void pauseForButtonEntropy(const int ButtonPin) {
Serial.println("Press button to start");
while (digitalRead(ButtonPin) == HIGH) { // wait for button press
; //do nothing
}
while (digitalRead(ButtonPin) == LOW) { // protect against button initially being pressed
; //do nothing
}
// comment this line to inhibit re-seeding:
randomSeed(micros() + random()); // inject entropy, rather than overwrite entropy -- good for repeated use
}
void setup() {
Serial.begin(115200);
pinMode(ButtonPin, INPUT_PULLUP);
}
void loop() {
pauseForButtonEntropy(ButtonPin);
Serial.println(random());
//delay(500); // simple rate-limiting -- need some sort of debouncing for noisy buttons
}Note that the "Bounce" attribute is turned off for both input devices