How to get pseudorandom uniformly distributed integers in C sufficient for statistical modeling?

I am writing a Monte Carlo simulation, and I will need a lot of random bits to generate integers evenly distributed over {1,2, ..., N}, where N <40. The problem with using the C function randis that I would have spent a lot of beautiful bits using standard technique rand % N. What is the best way to generate integers?

I do not need cryptographically secure random numbers, but I do not want them to distort my results. Also, I am not considering downloading a batch of bits from random.org for a solution.

+3
source share
3 answers

rand % N ; , RAND_MAX + 1 N.

, N, , RAND_MAX, , . . 50%.

+2

oli-:

, , , (, upper (log2 (n))).

, . (sp?) , (. ), .

( , , ref).

, , ( , ).

+1

Imagine rand40 in the database and take the numbers as numbers. Drop any incomplete digits i.e. Drop the first digit if it does not have a full range [0..39], and leave all the random number if the first digit takes the highest possible value (for example, if RAND_MAX is the base40, 21 23 05 06, drop all numbers with the highest base- 40 in number 21).

0
source

All Articles