# q:uniform distributed mapping func

• Jun 25th 2012, 04:21 AM
dooms
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!
• Jun 25th 2012, 05:54 AM
mfb
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.
• Jun 27th 2012, 02:17 PM
awkward
Re: q:uniform distributed mapping func
Quote:

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.
• Jun 28th 2012, 03:09 AM
dooms
Re: q:uniform distributed mapping func
yes! that was a dumb question by me. nothing's really random.

% n+1 will do.

sweet, thanks.