Home All Groups Group Topic Archive Search About

Random first random number

Author
20 Sep 2005 7:10 AM
LurfysMa
What is the recommended way to generate a random first random number
using the Rnd function?

I tried using the entire value of Now,

?rnd(-cdbl(now))

but I kept getting the same first random number.

After a little experimenting, I discovered that Rnd returns the same
first random number for about 5 minutes and 38 seconds:

?rnd(-cdbl(cdate("09/18/05 23:57:10")))
0.225379
?rnd(-cdbl(cdate("09/18/05 23:57:11")))
0.225379

?rnd(-cdbl(cdate("09/18/05 23:57:12")))
0.2146977
?rnd(-cdbl(cdate("09/18/05 23:57:13")))
0.2146977
....
?rnd(-cdbl(cdate("09/19/05 00:02:47")))
0.2146977
?rnd(-cdbl(cdate("09/19/05 00:02:48")))
0.2146977

?rnd(-cdbl(cdate("09/19/05 00:02:49")))
0.2040164
?rnd(-cdbl(cdate("09/19/05 00:02:50")))
0.2040164
....
?rnd(-cdbl(cdate("09/19/05 00:08:25")))
0.2040164
?rnd(-cdbl(cdate("09/19/05 00:08:26")))
0.2040164

?rnd(-cdbl(cdate("09/19/05 00:08:27")))
0.1933351
?rnd(-cdbl(cdate("09/19/05 00:08:28")))
0.1933351

Is this because Rnd only uses a single precision value? and the
difference gets lost for less than 5:38 (338 seconds)?


Anyway, what is the recommended way to get a random first random
number?

--
For email, use Usenet-20031220@spamex.com

Author
20 Sep 2005 7:38 AM
Mike Williams
"LurfysMa" <invalid@invalid.invalid> wrote in message
news:em6vi11t9pjbsfcioc2ududvud54piibn9@4ax.com...

> Anyway, what is the recommended way to get
> a random first random number?

Have a look at the Randomize function. Here is what the help file says about
it:

Randomize uses number to initialize the Rnd function's random-number
generator, giving it a newseed value. If you omit number, the value returned
by the system timer is used as the new seed value. If Randomize is not used,
the Rnd function (with no arguments) uses the same number as a seed the
first time it is called, and thereafter uses the last generated number as a
seed value. Note: To repeat sequences of random numbers, call Rnd with a
negative argument immediately before using Randomize with a numeric
argument. Using Randomize with the same value for number does not repeat the
previous sequence.

In your case you can simply use Randomize Timer (or just Randomize). Don't
use it repeatedly though. Use it just once at the start of your program. For
example, use it just once in your Form's Load event (if the Form is your
startup object).

Mike
Author
20 Sep 2005 7:46 AM
LurfysMa
On Tue, 20 Sep 2005 08:38:40 +0100, "Mike Williams"
<M***@WhiskyAndCoke.com> wrote:

>"LurfysMa" <invalid@invalid.invalid> wrote in message
>news:em6vi11t9pjbsfcioc2ududvud54piibn9@4ax.com...
>
>> Anyway, what is the recommended way to get
>> a random first random number?
>
>Have a look at the Randomize function. Here is what the help file says about
>it:

Yes. I knew that, too, but got carried away with trying to figure out
why the date argument wasn't working.

I just like to make things harder. ;-)

Thanks

Show quoteHide quote
>Randomize uses number to initialize the Rnd function's random-number
>generator, giving it a newseed value. If you omit number, the value returned
>by the system timer is used as the new seed value. If Randomize is not used,
>the Rnd function (with no arguments) uses the same number as a seed the
>first time it is called, and thereafter uses the last generated number as a
>seed value. Note: To repeat sequences of random numbers, call Rnd with a
>negative argument immediately before using Randomize with a numeric
>argument. Using Randomize with the same value for number does not repeat the
>previous sequence.
>
>In your case you can simply use Randomize Timer (or just Randomize). Don't
>use it repeatedly though. Use it just once at the start of your program. For
>example, use it just once in your Form's Load event (if the Form is your
>startup object).
>
>Mike
>
>


--
For email, use Usenet-20031220@spamex.com
Author
20 Sep 2005 7:44 AM
LurfysMa
On Tue, 20 Sep 2005 00:10:10 -0700, LurfysMa <invalid@invalid.invalid>
wrote:

>What is the recommended way to generate a random first random number
>using the Rnd function?

OK, I got so caught up in trying to figure out why the date wasn't
working, I overlooked the obvious.

Just use Randomize, right?

I would still like to know why the date doesn't work as a negative
argument to rnd().


--
For email, use Usenet-20031220@spamex.com
Author
20 Sep 2005 9:58 AM
Tony Proctor
Probably not a coincidence that 5 min and 38 sec is 1/256'th of a day.
Sounds like it the result of a bit-wise shift in the algorithm that uses
that numeric argument.

        Tony Proctor

Show quoteHide quote
"LurfysMa" <invalid@invalid.invalid> wrote in message
news:e9fvi11m500v5losrs3pgjcfg7dd1q6gdl@4ax.com...
> On Tue, 20 Sep 2005 00:10:10 -0700, LurfysMa <invalid@invalid.invalid>
> wrote:
>
> >What is the recommended way to generate a random first random number
> >using the Rnd function?
>
> OK, I got so caught up in trying to figure out why the date wasn't
> working, I overlooked the obvious.
>
> Just use Randomize, right?
>
> I would still like to know why the date doesn't work as a negative
> argument to rnd().
>
>
> --
> For email, use Usenet-20031220@spamex.com
Author
20 Sep 2005 3:30 PM
Jim Mack
LurfysMa wrote:
> On Tue, 20 Sep 2005 00:10:10 -0700, LurfysMa <invalid@invalid.invalid>
> wrote:
>
>> What is the recommended way to generate a random first random number
>> using the Rnd function?
>
> OK, I got so caught up in trying to figure out why the date wasn't
> working, I overlooked the obvious.
>
> Just use Randomize, right?
>
> I would still like to know why the date doesn't work as a negative
> argument to rnd().

I'm sure it's as you suspect -- the argument to Rnd() is converted to a Single, which uses fewer bits.  And I believe that the single is mapped to a 24-bit integer internally -- max value 16777216 -- which is used as the highest 24 bits of a 32-bit integer.

And I guess that would account for there being only 1 new Rnd for every 256 possible Double seed values (16777216 << 8 leaves the lowest 8 bits empty).

Rnd() is only nominally a single, since what's calculated is a 24-bit integer value, which is then mapped to the mantissa of a single.

You'll know if you grasp this, when you can explain the result of this experiment:

? (rnd(-2^23) - rnd(-2^24))

:-)

--
        Jim