Re: random numbers' function
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 23 Jan 2009 19:46:36 -0500
Richard Heathfield wrote:
James Dow Allen said:
Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
Tagore said:
How to make a function to generate random numbers between 1 to
7 using another function which generate random numbers between
1 to 5?
Roll twice. ... If you got 22, 23, 24, or 25,
discard the whole thing and start again.
And therefore not guaranteed to terminate, at least with a perfect
random generator.
Very true - but consider the odds!
I have finally realized what was wrong with my original "sum 7
calls and divide by 5" routine. We can correct this quite easily
by:
Make the random call return 0..4 in place of 1..5.
Make that call return reals, with the fractional part equilikely.
Keep the sum in a real.
It will be in the range 0 .. 35, with a full 35 forbidden.
Divide by 7.0.
The result is a number from 0 through 7, with a full 7 forbidden.
Take the integer, and add 1. The result is 1 through 7.
If necessary use two calls to rand to form the integer and the
fractional part of the 1..5 (or 0..4) results.
The point is that the original random calls have created 7
intervals, from:
0 .. <1.0
1.0 .. <2.0
2.0 .. <3.0
etc to
6.0 .. <7.0
and I see no biases anywhere.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
.
- Follow-Ups:
- Re: random numbers' function
- From: gw7rib
- Re: random numbers' function
- From: Mark Wooding
- Re: random numbers' function
- From: Ben Bacarisse
- Re: random numbers' function
- From: Kai-Uwe Bux
- Re: random numbers' function
- References:
- random numbers' function
- From: Tagore
- Re: random numbers' function
- From: Richard Heathfield
- Re: random numbers' function
- From: James Dow Allen
- Re: random numbers' function
- From: Richard Heathfield
- random numbers' function
- Prev by Date: Re: More game physics glitch questions
- Next by Date: Re: random numbers' function
- Previous by thread: Re: random numbers' function
- Next by thread: Re: random numbers' function
- Index(es):
Relevant Pages
|