randomSeed(analogRead(0)) - what does it do?

My code works perfectly fine both with and without this line of code, so what exactly does it do? Can someone a little bit smarter than me please explain what I'm missing.


The randomSeed function is used to initialize the beginning of the sequence of random numbers generated by the random function. The analogRead on a disconnected (floating) input is supposed to return a relatively random nimber to seed the pseudo random number generator.

Okay thanks mate, it makes a little more sense now.

Perhaps I can make things a little clearer for you. The random() function is not random. Like most "random" functions, it may appear random to us humans, but it is actually following a sequence of "pseudo" random numbers that can be predicted and repeated if you know the algorithm and the seed. For most proposes, this is fine. When encrypting secret data, this is a problem, because if any person knows the algorithm and can find out the seed, they can decrypt the message.

The other time this is a problem is in a game. If the same seed is used every time, a player can learn the sequence and use this to their advantage.

If the code performs an analogRead() on an unconnected, floating pin, this gives a result which is genuinely unpredictable. No algorithm can predict that result, because the pin is free to pick up signals from anywhere, either local or from the other side of the solar system. If this result is used as a seed, the random number generator will not be predictable, because even if the algorithm is known the seed is not.