Is there an unused Analog Pin for randomSeed() function?

I’m trying to do some randomization on the Arduboy, but I’ve learned from the Arduino website that you need to seed the random number generator with a random number to get new random numbers every time. Apparently the best way to get a random number on the Arduino is via an unused Analog pin.

Are there any unused analog pins on the Arduboy we could use with the randomSeed() function to get some real random numbers on the Arduboy?

Is there a better way?

I’m curious about this too.

If you’d like to know another method for doing a random seed though…one really good way is to wait for the player to press a button to begin the game, and use the current micros() (microseconds since the program started running) at that exact moment. It’s nearly impossible for players to press the button at the exact same time every time.

If you’re worried about players holding down the button as soon as they turn on the Arduboy, you could check to see if it is already pressed as you’re booting up and don’t count it as a press until they have let go of it first.

1 Like

I should have my devkit tomorrow and adding a setupRandom() or similar function is high on my list. It would draw from micros, analog pin, battery voltage, configurable settings like brightness, etc. Anything we can possibly use as a source of entropy.

1 Like

The only ones in use that I know of, so far, are A0 and A1 for the A and B buttons, and A2 for the piezo speaker. There could be others used that we haven’t been told about, or will be added to the final design, like (hopefully) battery level monitoring. If you use a #define or const int to specify the pin to use, at the start of your sketch, and document it with a comment, then it’s easy to change later if necessary.

Why not write a sketch that reads many of them and displays the value returned for each? Put this in the loop() function with a delay of a second or so at the end. Then, pick one that looks good.

The wait for button approach, that @unclesporky described, also works well, especially for the Arduboy since most sketches will probably be games and have a press any key to start or similar action.

I think using one or both of the two methods above would work well enough. Writing your own function to gain just a bit more entropy would likely be a waste of time and code space.

I think it makes sense to have an easy to call function in the core lib vs every developer having to learn how to setup proper entropy. If they don’t use the method then it doesn’t hurt anything. But I think most people will. Most people look at the examples and go from there.

Anyone programming for Arduinos or compatibles should become familiar with, or know how to search for the existence of, core Arduino functions. How to use the core randomSeed() function along with an unused analog pin to generate entropy is described in the documentation for the core random() function.

Rather that adding a function specific to the Arduboy library, which would require documenting, and be one more thing for a programmer to learn or discover, I think it would be easier to just document a recommended unused analog pin that would work well with the already available randomSeed() function, when used in Arduboy sketches.

1 Like

Out of curiosity I assume you’re a member of the Kickstarter?

Yes, but under a different name, which you shouldn’t have trouble discovering via a Google search. :wink:

I’ve submitted a pull request that adds initRandomSeed() to the core library. It uses micros(), the current voltage, and the current temperature reading as sources of entropy to seed the random number generator.