Hello, Is there a way to have real different random numbers? For the moment, I'm using the rand() function, but this one is generating the same sequence over and over again. I have to unpower my application to have a new series of random numbers. I'm now 'experimenting' (what's in a word...) with the following construction:
. . . srand( rand() ); . . . RandomNumber = ( rand() / 256 ); . . .
The whole subject of random numbers is vast. Any random number sequence produced by a deterministic process must pseudo-random. If you are looking for pseudo-random number generators that are good (in the statistical sense try this web-site: http://mandala.co.uk/links/random/ No matter how good the generator may be, it is still deterministic. For really random numbers, you will have to find a source of randomness that the micro can access and either use this for all your random numbers or to seed a good generator. Typical sources include timing mechanical and user input events.
You need some sort of genuinely random number to act as a "seed". How about capturing a timer register at the moment that the user pushes a button, or some other event which is asynchronous to your code?
"Typical sources include timing mechanical and user input events." Or electronic noise; eg, http://www.8052.com/forum/read.phtml?id=23959 (it'd be worth reviewing the whole of that thread!)
"my version" of what Richard is suggesting is as follows: "inchar" becomes non-zero when the interrupt detect a character from the keyboard display "select" then while (!inchar) rand++; erik
I have the same question about rand() and srand(),my god!!!
"I have the same question" So the answer will be the same; it is already before your very eyes!!!
As long as you ask the forum exactly the same question to start, the sequence of replies you get will be exactly the same. A slightly different question would produce a different sequence, though. It's all about where you start. The answer generator on the board has some statistical flaws, though. My analysis of the threads shows an extremely high rate of appearance of the value "Have you read the Manual?". It's not a uniform distribution at all.
Nobody said this forum was a random answer generator! "an extremely high rate of appearance of the value "Have you read the Manual?". It's not a uniform distribution at all." That's probably because the problem domain doesn't have a uniform distribution...!