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() ????
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.
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...
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.
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.