// randomSeedEntropyInjection
// https://wokwi.com/projects/428238528418621441
const int injectPin = 2;
void setup() {
pinMode(injectPin, INPUT_PULLUP);
Serial.begin(115200);
Serial.println(F(R"(This sketch injects user-based entropy from
serial input into the Arduino RNG by using
randomSeed(random() + micros()) when the switch is closed.
Without injection, random() starts with 16807,282475249,1622650073,...
With injection, it will advance to somwhere else in the 2^31-1 cycle of numbers.)"));
Serial.println("Hit Enter to begin...");
while (Serial.available() == 0 ) {} // wait for input
if (digitalRead(injectPin) == LOW) {
randomSeed(random() + micros()); //inject some user-based randomness
}
}
void loop() {
if (Serial.available()) {
Serial.println(random());
Serial.readStringUntil('\n'); // consume line
if (digitalRead(injectPin) == LOW) {
//inject more user-input-based randomness
randomSeed(random() + micros());
//randomSeed(0);
//randomSeed(1);
//randomSeed(16807);
}
};
}