# Thread: q:uniform distributed mapping func

1. ## q:uniform distributed mapping func

hi all,
need some guidance
how can i produce a uniform distributed mapping function, from integers(let's assume less than 2^31) to integers between 0..n?

help would be appreciated!

2. ## Re: q:uniform distributed mapping func

There is no general way to uniformly map all integers from 0 to m to the set 0...n unless (n+1) is a divisor of (m+1). In this case, just take the number mod (n+1).
However, you can do the following: Determine the largest k such that k*(n+1)<=m. If the random number is <k*(n+1), use this number mod (n+1), otherwise get a new random number.
This is not the most efficient way to use your source of randomness, but for small n the difference is negligible.

3. ## Re: q:uniform distributed mapping func

Originally Posted by dooms
hi all,
need some guidance
how can i produce a uniform distributed mapping function, from integers(let's assume less than 2^31) to integers between 0..n?

help would be appreciated!
Maybe just try reducing the integer modulo n+1.

4. ## Re: q:uniform distributed mapping func

yes! that was a dumb question by me. nothing's really random.

% n+1 will do.

sweet, thanks.