32-bit random numbers

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

32-bit random numbers

Mark Morgan Lloyd-5
Am I correct in interpreting the documentation as implying that if I
want to get a full-range 32-bit unsigned random number I have to use
something like Random($100000000) ?

Internally, that gets a 64-bit random by calling genrand_MT19937 twice
and throwing away one of the results. Not only does that waste time, but
it also means that if I use a "well known" seed I only see every other
value from the expected sequence e.g. http://oeis.org/A221557

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 32-bit random numbers

Peter
On 21/04/17 10:11, Mark Morgan Lloyd wrote:
> Am I correct in interpreting the documentation as implying that if I
> want to get a full-range 32-bit unsigned random number I have to use
> something like Random($100000000) ?
>
> Internally, that gets a 64-bit random by calling genrand_MT19937 twice
> and throwing away one of the results. Not only does that waste time, but
> it also means that if I use a "well known" seed I only see every other
> value from the expected sequence e.g. http://oeis.org/A221557
>

For a 32bit unsigned, I would be inclined to use random to a int64, and
mask out the upper bits.

You could also write an XORShift generator if you are prepared to forgo
the presence of zero in the sequence.


Peter.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 32-bit random numbers

Mark Morgan Lloyd-5
On 21/04/17 17:00, Peter wrote:
> On 21/04/17 10:11, Mark Morgan Lloyd wrote:> Am I correct in interpreting the documentation as implying that if I> want to get a full-range 32-bit unsigned random number I have to use> something like Random($100000000) ?> > Internally, that gets a 64-bit random by calling genrand_MT19937 twice> and throwing away one of the results. Not only does that waste time, but> it also means that if I use a "well known" seed I only see every other> value from the expected sequence e.g. http://oeis.org/A221557>
> For a 32bit unsigned, I would be inclined to use random to a int64, andmask out the upper bits.
> You could also write an XORShift generator if you are prepared to forgothe presence of zero in the sequence.

Trying to read successive 32-bit randoms only gives you the odd members
of the Mersenne Twister sequence. Easily fixable, I'll put a patch on
Mantis later.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...