Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

 Messages: [ Previous | Next ]
 briggs@encompasserve.org Posts: 404 Registered: 12/6/04
Re: Random numbers
Posted: Dec 21, 2007 9:42 AM

In article <tqenm3pq5fh6p8raf7ukplqhrflfrfhjuq@4ax.com>, quasi <quasi@null.set> writes:
> On 21 Dec 2007 14:53:38 +0200, Phil Carmody
> <thefatphil_demunged@yahoo.co.uk> wrote:
>

>>quasi <quasi@null.set> writes:
>>> On 21 Dec 2007 14:33:08 +0200, Phil Carmody
>>> <thefatphil_demunged@yahoo.co.uk> wrote:
>>>

>>> >John <iamachamp@gmail.com> writes:
>>> >> 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.

>>> >
>>> >int rand1to7i()
>>> >{
>>> > int r1=rand1to5i();
>>> > if(r1==5) { r1+=rand1to5i()/2; }
>>> > return r1;
>>> >}

>>>
>>> Fatally flawed.

>>
>>Wrong. Care to back up your claim? Between you and me, I
>>reckon you've not thought about my response enough.

>
> Yes, do a simulation, and then _you_ think about it.
>

>>> >double rand1to7d()
>>> >{
>>> > return rand1to5d()+(rand1to5d()-1)/2;
>>> >}

>>>
>>> Inefficient -- uses 2 RNG calls, instead of 1.

>>
>>Wrong. If you stretch the numbers out, you'll leave gaps
>>between them, these aren't actual reals, you did realise
>>that, right? You *need* 2 calls to ensure that you leave
>>no gaps.

>
> Forget gaps -- your method is biased!

The problem statement does not require the output distribution
to be unbiased, gap-free or that it even cover the stated range.

Accordingly, the following code solves the problem as written:

double rand1to7d() {
return rand1to5d();
}

If you wish to write code that produces a distribution that
covers every model number in the stated range with probability in
proportion to the measure of the set of real numbers in the range
that are nearer to the given model number than to any other in the
range, you'll probably need some details about the underlying floating
point model.

At this level of detail, it becomes clear that rand1to5d must select
from a finite set of possibilities with probabilities that are all
multiples of a fixed finite probability.

Barring lucky happenstance it follows that any implementation
of rand1to7d using rand1to5d as its source of randomness must involve
a loop.

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