The rand() function in the stdlib.h is suppost to generate random numbers. However in C programming the random number is generated from a variable eg time, date,.. Then in Keil, and the controller, how does this rand() function work.. Cause i tried using it, it worked ok.. The problem is everytime the controller is reseted, it generates the same set of random numbers. And if u randomize again, it will just become a patern instead of a random value.. Example: after reset: 3,5,8,1,4,...etc etc.. after the reset button again... 3,5,8,1,4,...etc etc.. Does this mean there is no other way to generate random numbers?
http://www.8052.com/forum/read.phtml?id=78660
It's generally considered a feature for a (pseudo) random number generator to be able to reproduce the same sequence of random numbers. This feature allows, for example, replication of the results of a simulation run, or the result of some sort of Monte Carlo algorithm. The "seed" for the PRNG controls which sequence you get. See the standard C library function srand(). You'll want to call srand() with different values every time you start up, and perhaps every so often even after you start up. Truly random numbers are occasionally needed for some applications, such as cryptography or really heavy-duty statistics. For those apps, you need hardware RNGs, or at least the collection of entropy from as many different sources as you can find in your system. The same sources are also good places to get seeds for srand().
"The 'seed' for the PRNG controls which sequence you get." Does it control which sequence you get, or just where you start in the same sequence?
Hm. I suppose the right answer to that question depends on the math for the exact type of generator used. As far as I know, the bright guys choosing the generator polynomials for LFSRs or terms for a linear congruential method usually find it desirable to generate a maximum length sequence. Sometimes you can live with a shorter sequence to speed things up. Both these types of algorithms have "loops" where the generated sequence will start to repeat. And they both can generate multiple, independent loops. I'm not sure if you can hop into two different loops with a linear congruential algorithm only by varying the seed, though, without modifying the other three terms. LFSRs also have to avoid the case of the all-zeroes value, at which point they would get stuck producing an endless stream of zeroes. The easiest way to do that is just force the state back to the seed, or some predefined value, the latter of which seems likely to trap you in a single loop even with different seeds. But in short, I don't really know the answer for certain. Neither of these methods are really state of the art, though, and aren't really suitable for hardcore random number generation. The Mersenne Twister seems to be trendy for non-crypto use: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html If you want REALLY random numbers: http://www.fourmilab.ch/hotbits/