We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I would like to know on which method Keil rand() function generates the random values.Are they using LCM or Linear Feedback shift register or any other method? Please help me in this. Thanks Gopi
Run in the simulator, set a breakpoint r = rand(); and then use the disassembly window. Single set into the library function This step already used(to check the coding in disassembly window)to see what they are doing for generating random function.I could able to understand assembly coding and able to decode it. But still I am not clear which method they used to generate the random values.
To give a more complete answer, you first need to understand a little more about Linear Feedback Shift Registers. Start here: http://www.newwaveinstruments.com LFSRs can be implemented in either hardware or software, obviously the Keil random function is in software, but it is easier to conceive it as a bit of hardware. The Keil random function uses a Fibonacci 32-bit shift register. Looking back at the hurried notes I took when looking at rand(), the Keil LFSR seems to have just two taps: at g2 and g30. LFSRs are usually designed to be of maximum length - ie the maximum number of times the LFSR can be clocked before it will return to the same state - for an LFSR of n bits the maximum length will be (2^n)-1. In many cases, this means that the length of the sequence will be conveniently prime. The web-site referenced above gives tap lists for LFSRs that produce the maximum possible length sequences from LFSRs of various sizes, but the Keil choice of taps is not listed. So, I dont know what the length of Keil's LFSR is. It may be that the LFSR will generate several sequences of different lengths - depending on the initial state determined by the seed. Another feature of a maximum length LFSR is that once initialised correctly, it will never get "stuck" in a particular state - so checking the state of the LFSR is unnecessary. For each call of rand(), the LFSR is clocked 15 times so that rand(). I guess that the reason being that the application may make use of up to 16 bits returned by rand(). Generally speaking, more taps is better (up to half the number of LFSR bits). All LFSRs are eqully deteministic of course, but more taps stirs the LFSR in more "unexpected" ways. A better rand()? My choice would be a 31-bit LFSR (because (2^31)-1 is prime) and a Galois implementation because it is easy to implement many taps with no extra software overhead. The new rand() would take a parameter that indicates the required number of bits; rand() will then clock the LFSR the required number of times and the application should n least significant bits. I don't see any reason why rand() should not return 31 bits.
"I don't see any reason why rand() should not return 31 bits." I don't know anything about random numbers but I can answer this: ANSI defines rand() as returning an int, and Keil seem to try and be compliant wherever possible. Stefan
See: http://www.keil.com/forum/docs/thread3202.asp
Hi Graham Gole, Thanks for your kind reply! Need your email ID to keep in touch. Regards, Gopi