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?
Are you setting the random number generator seed value using the srand function? Jon
"The rand() function in the stdlib.h is suppost to generate random numbers." Oh no it is not!! Read that Manual page again, carefully: "Description: The rand function generates a pseudo-random number between 0 and 32767. Return Value: The rand function returns a pseudo-random number." http://www.keil.com/support/man/docs/c51/c51_rand.htm You need to be absolutely clear from the outset that you cannot generate a truly random number by software alone.
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/
Means it does not really generate a random number rather than a set of psedovalues. To implement a random value in a system every time it is boot up, we must then use a memory element to store and increament a value each time a system is started for the srand() value. This is to ensure each different set of psedorandom value is generated every time the system is booted up. Maybe at first i didn't understand what psedo number means.. Thanks.
How does it work? You may find this link informative: http://www.programmersheaven.com/zone5/cat27/32219.htm
a true story about the importance of "pseudo". The J1708 standard specify that after a collision a reandom delay is to be inserted before the nest attempt. Two devices, both using the Keil rand(); Keil, PLEASE rename it pseudo_rand(); collided, since they both added the same delay every time the collision that started some time during a "let it run the weekend, we'll see monday" was still occuring. Erik
"Keil, PLEASE rename it pseudo_rand();" Unfortunately, the name "rand()" is defined by the ANSI/ISO standard.
It's generally a good idea to re-seed your PRNG every so many collisions, just to avoid the sort of situation Erik describes. Initial seeds usually involve the MAC address, serial number, and other such information unique per device. Packet arrival times are a common source of accumulated entropy for later seeds for networked devices. User input (keystroke times) can be useful if you have a user that inputs anything. There are schemes for fairly simple hardware devices for generating random bits, involving amplified resistor noise or tunneling in diodes. You don't really need a nuclear research lab. A Web search can lead you to designs for such things.