Re: random integer
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Wed, 25 Apr 2007 07:57:10 +0000
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.
.
- Follow-Ups:
- Re: random integer
- From: SucMucPaProlij
- Re: random integer
- References:
- random integer
- From: bob
- Re: random integer
- From: Phillip
- random integer
- Prev by Date: Re: language
- Next by Date: Re: random integer
- Previous by thread: Re: random integer
- Next by thread: Re: random integer
- Index(es):
Relevant Pages
|