Re: random integer




"Richard Heathfield" <rjh@xxxxxxxxxxxxxxx> wrote in message
news:ea6dnVQWNKf-lLLbnZ2dnUVZ8trinZ2d@xxxxxxxxx
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.


if RAND_MAX is 100015 then numbers 0-5 will appear 10002 times, and 6-9 will
appera 10001 times.



.



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
    ... Phillip said: ... 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 ...
    (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)