Re: random integer
- From: "SucMucPaProlij" <buji@xxxxxxx>
- Date: Wed, 25 Apr 2007 10:22:37 +0200
"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.
.
- References:
- random integer
- From: bob
- Re: random integer
- From: Phillip
- Re: random integer
- From: Richard Heathfield
- random integer
- Prev by Date: Re: random integer
- Next by Date: Re: random integer
- Previous by thread: Re: random integer
- Next by thread: Re: random integer
- Index(es):
Relevant Pages
|