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:
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
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
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
0 .. <1.0
1.0 .. <2.0
2.0 .. <3.0
6.0 .. <7.0
and I see no biases anywhere.
[mail]: Chuck F (cbfalconer at maineline dot net)
Try the download section.
- 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