# Given a 32-bit unsigned ran num, how to manipulate value in a certain range [low, high]?

Like the title says, I'm using a random number generator on a Freescale Coldfire chip and it returns a 32 bit unsigned value. As far as I know, there is no way to configure the generator to limit the range. What would be the best way to manipulate the number to be in the accepted range?

I was thinking of modding the number by the high range value but I would still have to deal with the lower bound.

## Solutions

This C FAQ article How can I get random integers in a certain range? explains how to properly generate random numbers in range `[M,N]` basically the formula you should use is:

``````M + (random number) / (RAND_MAX / (N - M + 1) + 1)
``````

Stephan T. Lavavej explains why doing this is still not going to be that great:

From Going Native 2013 - rand() Considered Harmful

If you really care about even distribution, stick with a power of 2, or find some routines for dithering.

Yes, the traditional way is to MOD by the range, and this will be fine for many ordinary uses (simulating cards, dice, etc.) if the range is very small (a range of 52 compared to the 32-bit range of your generator is quite small). This will still be biased, but the bias will be nearly impossible to detect. If your range is bigger, the bias will be bigger. For example, if you want a random 9-digit number, the bias will be dreadful. If you want to eliminate bias altogether, there is no alternative but to do rejection sampling--that is, you must have a loop in which you generate random numbers and throw some away. If you do it right, you can keep those extra numbers needed to a minimum so you don't slow things down much.

Tags: C / Random / Coldfire

## Similar questions

Given `int num`, how do `num` , `&num`, `&num` differ?
I read in a C book that for an array num the term num is equivalent to &num. This concept was working fine for me but when I wrote this program shown below I again got confused. output is: If num is identical to &num then why there is a difference in their size? And what is this third type of term &num? I know it is showing a g...
Why is {typedef int* PTR;const PTR p=&num;} not equivalent to "const int* p=&num" but to "int *const p=&num"?
This thing was partly touched upon in another question on SO, but somewhat casually as it was not the main question. As my confusion still persists, I am putting it in a separate question. Why are the following two statements equivalent to int* const p=&num and not const int* p=&num when the latter seems more logical and intuitive? What are...
DWORD variable with low/high word and low/high byte
How in C can we read and make DWORD variables with a low and high word and low and high byte?
MSP430F5418 port interrupt occurs for both high-to-low and low-to-high transitions
I had set MSP430F5418 P2.5 for high to low transition. But I am getting interrupts for both low-to-high and high-to-low transitions. Please my code snippet below.
In a call stack that grows from high to low address, are the data elements saved from low memory address to high memory address or vice versa?
In architectures where the call stack grows in memory from high memory address to low memory address, suppose the stack pointer is at a higher memory address Y initially, and after an integer argument is saved onto it, the stack pointer goes to a lower memory address X. Then would the first byte of the integer argument be at X(lower address), or at...
Is there an efficient way of doing 32-bit bitwise rotation separately on the high and low 32-bit parts of a 64-bit number?
I'm currently working in C/C++, and I have a uint64_t. I need to do a bitwise rotation on the top 32 bits and the bottom 32 bits separately. So for example, if my input is and I need to rotate 2 bits to the right, the proper output is The obvious method is to make a temporary 32-bit number and do the rotation operations on that separately, but is t...