This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Random Numbers in a limited range

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.

Parents Reply Children
  • "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