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?
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/