Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Random numbers
Replies: 64   Last Post: Dec 24, 2007 1:04 PM

 Messages: [ Previous | Next ]
 quasi Posts: 12,057 Registered: 7/15/05
Re: Random numbers
Posted: Dec 21, 2007 8:49 AM

On Fri, 21 Dec 2007 07:15:28 -0500, quasi <quasi@null.set> wrote:

>On Fri, 21 Dec 2007 03:16:21 -0800 (PST), John <iamachamp@gmail.com>
>wrote:
>

>>Given a function that returns a random number between 1-5, write one
>>that returns a random number between 1-7 for the case when it should
>>be integer and for the case it can be real.

>
>Here's a simple way ...
>
>Random integers:
>
>Assume you have a function which returns a random integer (uniformly
>distributed) from 1 to 5. Generate 2 such random integers, x1, x2, and
>let x = x1 + 5*(x2 - 1). Then x is a random integer from 1 to 25. If x
>is greater than 21, reject x and try again, otherwise, return (x mod
>7) + 1
>
>Random reals:
>
>Assume you have a function which returns a random real (uniformly
>distributed) from 1 to 5. Then generate one such random real, x say,
>and let y =(3/2)*(x-1) + 1. Then y is a random real from 1 to 7.

Any inflated "gaps" created by the factor 3/2 would be rather mild,
and probably not significant for most applications. However if you are
worried about gaps, here's a revised version which avoids inflated
gaps, but requiring 2 RNG calls.

Random reals (gap aware version):

Assume you have a function which returns a random real (uniformly
distributed) from 1 to 5. Let x be a random real from 1 to 5. Reject x
and retry if x >= 4. This yields a random real from 1 to 4. Generate
another random real w from 1 to 5. Let b = 0 if w < 3, b = 1
otherwise. Thus b is a random integer from the set {0,1}. Now let y =
x + 3b. Then y is a random real from 1 to 7.

quasi

Date Subject Author
12/21/07 Champ
12/21/07 quasi
12/21/07 quasi
12/21/07 Phil Carmody
12/21/07 quasi
12/21/07 quasi
12/21/07 Phil Carmody
12/21/07 quasi
12/21/07 Phil Carmody
12/21/07 Phil Carmody
12/21/07 quasi
12/21/07 quasi
12/21/07 Phil Carmody
12/21/07 quasi
12/21/07 Phil Carmody
12/21/07 Phil Carmody
12/21/07 quasi
12/21/07 Phil Carmody
12/21/07 quasi
12/21/07 Marshall
12/21/07 Phil Carmody
12/21/07 quasi
12/21/07 Phil Carmody
12/21/07 Marshall
12/21/07 briggs@encompasserve.org
12/21/07 William Elliot
12/21/07 quasi
12/22/07 William Elliot
12/21/07 Pubkeybreaker
12/21/07 b92057@yahoo.com
12/22/07 quasi
12/21/07 simple.popeye@gmail.com
12/21/07 simple.popeye@gmail.com
12/22/07 quasi
12/22/07 Gib Bogle
12/22/07 quasi
12/21/07 Marshall
12/22/07 simple.popeye@gmail.com
12/22/07 quasi
12/22/07 simple.popeye@gmail.com
12/22/07 quasi
12/22/07 quasi
12/22/07 quasi
12/22/07 simple.popeye@gmail.com
12/22/07 quasi
12/23/07 simple.popeye@gmail.com
12/23/07 simple.popeye@gmail.com
12/23/07 simple.popeye@gmail.com
12/23/07 simple.popeye@gmail.com
12/23/07 simple.popeye@gmail.com
12/22/07 simple.popeye@gmail.com
12/22/07 Herman Rubin
12/22/07 b92057@yahoo.com
12/22/07 quasi
12/23/07 b92057@yahoo.com
12/23/07 quasi
12/23/07 b92057@yahoo.com
12/24/07 quasi
12/24/07 quasi