randomSeed(analogRead(0));

i'm using video experimenter shield for arduino. I have downloaded a program for dispalying text on tv screen from nootropic design website. the program contains " randomSeed(analogRead(0)); " this function. i have gone through the arduino website where they have clearly mentioned about the advantages of using this function. My doubt is the random number generated by this function is to assigned to what ??? wheteher the value generated by this function is used by the computer as the location for a particular memory ??? and why this function has to be declared in void setup(() but not in void loop() ????

can anybody help please ????

That function just seeds the random number generator - it doesn't create a random number.

Is there something wrong with your question-mark key?

That function defines where the seed for the random number generator is picked up from. In this case, it will be from analog pin 0, which has to be left floatingfor this to make some sense. Since the pin is floating, the value that will seed the random number generator will never be the same thus making it truly random.

It needs to be done only one time, since it will only define where the seed comes from and not generate actual numbers.

Try looking for random number in C, the explanation is good enough and applicable in Arduino too.

Thanks for the replies...and sorry for that question marks

While it is common practice to seed the internal random generator this way, it doesn't really do a very good job. In practice, the analogRead(0) on an unconnected pin doesn't produce too many possible values, and even theoretically will only produce up to 10 bit (in practice much less), when the seed function is designed to use a 32-bit integer.

Here are some examples of what the randomSeed(analogRead(0)) used on an example Arduino;
// The more normal randomSeed(analogRead(0)) produces far less random seed
// values as showm in the following 25 examples:
// Seed value = 303
// Seed value = 326
// Seed value = 327
// Seed value = 326
// Seed value = 326
// Seed value = 328
// Seed value = 328
// Seed value = 328
// Seed value = 330
// Seed value = 328
// Seed value = 328
// Seed value = 329
// Seed value = 327
// Seed value = 328
// Seed value = 328
// Seed value = 329
// Seed value = 329
// Seed value = 329
// Seed value = 331
// Seed value = 331
// Seed value = 330
// Seed value = 331
// Seed value = 329
// Seed value = 329
// Seed value = 329

As you can see, in 25 samples, there are only seven different values, which differ by only a couple of low end bits...

Still better than randomSeed(3); ...

Any ideas on how to make it better?

bubulindo:
Still better than randomSeed(3); ...

Any ideas on how to make it better?

Search this forum for PRNG and PRNG seeders. There have been some interesting discussions, and lots of pointers to code that might give you better randomness.

... if you need it. Seeding the PRNG with the current date is a terrible thing, but it is a reasonably terrible thing to do if all you need is a reasonably different set of PRNs for each invocation. And you can reseed the PRNG over the lifetime of the program, if you need to select a reasonably different set of PRNs.

But if you need better PRNs, you can get them. You almost certainly need to use a third-party library.

bubulindo:
Still better than randomSeed(3); ...

Wanna bet?

Any ideas on how to make it better?

There are two approaches. One is to use a reliable source of entropy like @wanderson's library to generate a seed....
http://arduino.cc/forum/index.php/topic,108380.0.html

The other is to save the seed and apply a reasonable algorithm for advancing the value on the next reset...
http://arduino.cc/forum/index.php/topic,66206.0.html

Thanks for the links. :slight_smile:

thanks for the replies...was very much helpful

Another thread, with links to still more: http://arduino.cc/forum/index.php/topic,71841

On there you will find some potentially useful code to swizzle together a whole slew of analogue readings to get something with more entropy in it. It's still taking a chance that analogue reads of floating pins have useful noise on them, and that is simply not a completely safe assumption.

Is it necessary for this function randomSeed(analogRead(0)); to be preceded by
Serial.begin(9600); as seen here...

http://arduino.cc/en/Reference/RandomSeed

In short, is the analogpin 0 affected by Serial.begin?
Thanks.

They are completely independent.

As you can see, in 25 samples, there are only seven different values, which differ by only a couple of low end bits...

There are quite a few ways to get around that.

But the approach fails fundamentally because the stock analogRead() is incorrectly coded.