Entropy library question

DaveAK

Well-known member
I'm looking for a "good" random number solution in a non-cryptography way. Good just means that it has to be reasonably fast as I'll be using it a lot, and it shouldn't be obviously apparent that it's pseudo-random, even if it is. Initially I was using just the standard randomSeed() and random() functions and it was very obvious that it was repeating itself. I've switched to the Entropy library and just want to confirm my implementation because most of the discussion seems to be around it's effectiveness in cryptography based solutions, but also that it's pretty slow, (compared to what I'm not sure).

Here's what I've got:

Code:
void setup() {
  // Lots of set up stuff ...

  // ... and these two lines to seed the PRNG
  Entropy.Initialize();
  randomSeed(Entropy.random());
}

.. and then I just use the random() function from the Arduino library through the rest of my application. My understanding is that this gives a much better seed than say randomSeed(analogRead(someUnusedPin)), but is that all I need to get better randomness without going overboard?
 
That's probably fine for most non-crypto usage.

If you wanted to do better, you could call Entropy.available() each time you read from the pseudo random source. When Entropy has more actually random bits, you could xor them into the pseudo random seed.
 
Thanks Paul. Presumably calling available() and (re?)-seeding would have a performance impact, which may or may not be significant in my use case. I'll run with what I have for now as I think it will suffice, and then explore better options if need be. If the effect I was seeing earlier in development isn't noticeable anymore then I'll probably put my efforts in to other improvements until circling back to this one.
 
Back
Top