Re: random numbers' function
- From: "robertwessel2@xxxxxxxxx" <robertwessel2@xxxxxxxxx>
- Date: Thu, 22 Jan 2009 15:05:06 -0800 (PST)
On Jan 22, 6:35 am, gw7...@xxxxxxx wrote:
On 22 Jan, 10:47, "[Jongware]" <so...@xxxxxxxxxxx> wrote:
robertwess...@xxxxxxxxx wrote:
On Jan 21, 8:26 pm, CBFalconer <cbfalco...@xxxxxxxxx> wrote:
Tagore wrote:
How to make a function to generate random numbers between 1 to 7 usingSince 5 and 7 are prime, they are also mutually prime. Generate 7
another function which generate random numbers between 1 to 5?
calls to the 1..5 function, and add the results. Divide that sum
by 5. The result will be in the 1 to 5 range.
If you can do seven calls to the rand5 function, compute:
t = rand5()*(5**6)+rand5*(5**5)*rand5()*(5**4)+rand5*(5**3)*rand5()*
(5**2)+rand5*(5**1)*rand5()*(5**0);
And then generate five evenly distributed random numbers by repeatedly
dividing by seven and outputting the remainder. No wasted input bits,
either.
(Note: assumes that rand5() returns 0..4, and that the output is 0..6
- obvious adjustments required for base 1 inputs and/or outputs).
I've assumed the same thing, and came to exactly the same conclusion.
Since 5 and 7 have no common divider, you need to call the RNG 7 times,
to ensure a perfectly even distribution (at least, with roughly the same
distribution the RNG provides for 5 random numbers). Any 'clipping'
might distort the outcome.
The inverse is also true!
"How to make a function to generate random numbers between 1 to 5 using
another function which generate random numbers between 1 to 7?"
To ensure the same distribution (and, as James Dow Allen further
specifies, a terminating program), one should call the RNG 5 times, not
x times-until-less-than-5.
Chuck, Robert, Jongware - I'm afraid I think you are all wrong, in
that there is no way to produce a perfect distribution.
If you call the generator n times, then there are 5^n equally likely
possible outcomes. We want to make exactly 1/7 of these map onto an
output of 1 - which is impossible as 5^n is not a multiple of 7.
Calling the random number generator fewer times in certain
circumstances won't help, as this is equivalent to always calling it n
times and sometimes ignoring the later results. If the algorithm has a
maximum number of times to call the generator, then the outcome must
be skewed.
Absolutely correct. I was thinking of a different relationship, and
just pooched it by applying it to this problem.
.
- References:
- random numbers' function
- From: Tagore
- Re: random numbers' function
- From: CBFalconer
- Re: random numbers' function
- From: robertwessel2@xxxxxxxxx
- Re: random numbers' function
- From: [Jongware]
- Re: random numbers' function
- From: gw7rib
- random numbers' function
- Prev by Date: Re: school after all these years?
- Next by Date: Re: ch ess
- Previous by thread: Re: random numbers' function
- Next by thread: Re: random numbers' function
- Index(es):
Relevant Pages
|