I need a souce of randomness to generate a seed to pass to randomSeed. Where can I get this randomness from?

Well, I hear I can read random noise from an unconnected pin.

But I need to connect 9 LEDs and 9 buttons to the board. There seem to be enough pins for this purpose: digital pins 2-13 + analog pins 0-5 makes a total of 18 pins. But does this leave any free pin for a source of randomness?

There are digital pins 0 and 1 left free but I'm reading these should be left free if the board's USB socket is connected and I'm planning to power the whole device by connecting it to a computer through USB. Does this mean I'm supposed to just left pins 0 and 1 unconnected or is it also forbidden to read noise from them?

Or are there any other available sources of randomness? Can I use any other pins for this purpose?

I remember when I was showing this project to the instructor I specifically asked if I have enough pins, noting that I need a source of randomness in addition to 9 LEDs and 9 buttons. He said there are enough pins. Only sadly I forgot to ask how can I read random noise then...

What are my options to somehow obtain a random seed?

  • i would use the timing of your button presses to mix the internal seed state.
    – dandavis
    Jun 25 '19 at 20:04
  • @dandavis sadly I need to generate random numbers BEFORE the user is supposed to press any buttons.
    – gaazkam
    Jun 25 '19 at 20:05
  • 1
    you should use EEPROM to store the seed so that you eliminate the "no ent on boot" problem inherent in PRNGs. Readup on the fortuna algo on wiki for more info. You don't need to go THAT fancy, but applying some of the principles will solve your issue. You can also set the pins as analog inputs right away, scan them a few times, then set them back up as your device needs to run. You won't get a whole lot, but every bit (literally) counts.
    – dandavis
    Jun 25 '19 at 20:09
  • @dandavis Oh my... I definitely don't need cryptographical security :)
    – gaazkam
    Jun 25 '19 at 20:13
  • here is an interesting video m.youtube.com/watch?v=1cUUfMeOijg
    – jsotola
    Jun 25 '19 at 23:26

Whether you need a source of random noise depends on whether your project needs to not run repeatably. Most experiments - Monte-Carlo simulations, for example, benefit by being able to be re-run. In those cases, you need to be able to re-use the seed, and having an non-reproducible seed (and thus, experiment) is a disadvantage.

If you really need something that runs differently each time you run it, then a seed derived from user reaction time, or from some kind of electrical noise, even reading analog values from a floating input pin, should give you plenty of non-reproducibility.

  • brilliant: get the seeds from the environment. one can always use millis() as part of the seed as well.
    – tony gil
    Jun 25 '19 at 21:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.