Hi all!! I apologize my poor english. I need to generate a serie of random numbers utilizing the functions RAND e SRAND in a limited range, ex 0 to 7, 30 to 76, etc. How can I create these numbers?? Can anybody help me??? Thanks in advance. Mauricio.
To get a random number in a limited range, all you have to do is to take the modulus of a larger random number and possibly then apply an offset. For example, to get random numbers in the range 0 to 7:
r = rand() % 8;
"To get a random number in a limited range, all you have to do is to take the modulus of a larger random number and possibly then apply an offset." I seem to remember reading somewhere that the least significant bits of the return value of [a typical implementation of] rand() tend to be less 'random' than the most significant bits. The implication is that it is better to divide the value down to the range you want rather than using %.
I seem to remember reading somewhere that the least significant bits of the return value of [a typical implementation of] rand() tend to be less 'random' than the most significant bits. I think that this applies to Linear Congruential Generators (LCG) that were, and still are, common. The Keil library function uses a Linear Feedback Shift Register (LFSR) method. In the case of an LFSR, all the bits are equally pseudo-random so that, aside from portablility issues, it really does not matter which bits you take.
This whole thread is incorrect. The means discussed are not for random numbers but for pseudo-random numbers. Do not confuse the two. Erik