Re: random numbers' function



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.
.



Relevant Pages

  • Re: random numbers function
    ... calls and divide by 5" routine. ... Make that call return reals, ... Keep the sum in a real. ... The point is that the sum of uniform ...
    (comp.programming)
  • Re: sum R
    ... Please give some precise definition (usually, sum ... the sum over all the reals in a real interval. ... and you dont even understand tan. ... a 13yo who had just been introduced to basic geometry ...
    (sci.math)
  • 0.999...
    ... not for all of whom a decimal expansion exists. ... of a sum of an (infinite) series, ... it simply *defines* the sum as the limit. ... Thus, Earie allows some reals ...
    (sci.math)
  • Re: ******* TRY THESE SCI.MATH **********
    ... >> And then there's the items on blackboard 1. ... > what is the sum of numbers on the board? ... set up an equivalence of the reals with Cauchy sequences, ... S setMinus r = that set T such that every element ...
    (comp.theory)
  • Re: ******* TRY THESE SCI.MATH **********
    ... >> And then there's the items on blackboard 1. ... > what is the sum of numbers on the board? ... set up an equivalence of the reals with Cauchy sequences, ... S setMinus r = that set T such that every element ...
    (sci.math)