Re: random integer



Phillip said:

bob@xxxxxxxxxxxxxx wrote:

For instance, if you want a number between 0 and 31999, you can't
just do rand()%32000 since 0-767 will be twice as likely.

Why ?

Pretend that you have four-bit ints, RAND_MAX is 15, and you want a
random number in the range 0 to 9. This is the same problem, just
scaled down a bit.

R R % 10
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 0
11 1
12 2
13 3
14 4
15 5

As you can see, the numbers 0 to 5 are twice as likely to appear as the
numbers 6 to 9.

Now scale up.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Re: outlook express goes offline when switching identities
    ... outlook express will go offline and pretend ... > it is configuring for the next identity. ... It does this twice and then it ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)
  • Re: random integer
    ... Pretend that you have four-bit ints, RAND_MAX is 15, and you want a ... the numbers 0 to 5 are twice as likely to appear as the ... appera 10001 times. ...
    (comp.programming)
  • Re: calculations in the preprocessing stage
    ... Just demanding "has at least twice as many bits as" (as your code ... The first version of my code used char arrays and added them together ... version by using unsigned ints to store the numbers and perform the ... faster on the UVa computer, the one with the unsigned array and long ...
    (comp.lang.c)