SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By JonBDavidson
#199363
HI,

I'm trying to use the randomSeed(analogRead(0)); function to randomly generate numbers between 1 and 3. However, despite following the Arduino example using the randNumber variable, I get the same exact numbers almost every single time. Here is an example of what I get on the serial monitor:

1
2
1
1
2
2
3
3
3
3
3
3
2
1
1
etc.

It doesn't matter if I put delays in the code between readings or if I lengthen or shorten the delays, there is nothing connected to the analog A0 pin, and to make matters more mysterious, if i use while loops to keep the function from progressing until it generates a new number, it keeps generating the same numbers anyway. If I use If conditions to make sure the new number generated doesn't match the previous number generated, it ignores the If statements too, and continues to print the same numbers. Is randomSeed a viable random number generator function, or should I drop it for another one? Thanks for any input on this.
By jremington
#199369
You have to use a different value for the randomSeed argument every time, to get a different sequence.

It is not guaranteed that an unconnected or "floating" analog input will provide suitable numbers. Is A0 actually unconnected, in your case? Try printing out the value returned by analogRead(A0) to see if it ever changes.

Another approach is to start a loop counting when the program begins, stop it when someone presses a button and use the count for the seed.
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]