Quality of rand()

Elsewhere I saw someone making an argument and using a Perl program
to support it, one in which he calls rand() in a loop 2000000 times.
Now, in my native C, rand() typically falls apart statistically long
before that many iterations. Is there any guarantee in Perl on the
randomness and/or length of the period of rand?
"perldoc -f rand" doesn't tell.
David Harmon [ Fr, 11 April 2008 19:44 ] [ ID #1940914 ]

Re: Quality of rand()

"Newsgroup only please, address is no longer replyable."
<bad [at] example.invalid> wrote:
> Elsewhere I saw someone making an argument and using a Perl program
> to support it, one in which he calls rand() in a loop 2000000 times.

And then what? We can't critique the argument if we don't know what it is.

> Now, in my native C,

I don't have a native C. I doubt you do either. Your computer might.
If your computer's native C has crappy random number generators, I suspect
your perl will inherit that crappiness.

> rand() typically falls apart statistically long
> before that many iterations.

All psuedorandom number generators will fall about if your statistical
criteria are stringent enough and you are willing to spend enough time
finding the problem. So it is kind of meaningless to say that without
specifying exactly what type of falling apart you are looking for.

> Is there any guarantee in Perl on the
> randomness and/or length of the period of rand?
> "perldoc -f rand" doesn't tell.

I don't think perl makes any guarantees about anything.

By running ltrace on a simple program, I see that my perl calls drand48
to get its random numbers. Therefore, it inherits the same limitations as
discussed in "man drand48". I find this good enough, but if you don't
there are modules for Perl that try to give you cryptographic strength
random numbers. I suspect they are God-awful slow.

perl -le 'foreach(1..2e6) {die if $h{rand()}++}'

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
xhoster [ Fr, 11 April 2008 20:15 ] [ ID #1940917 ]

Re: Quality of rand()

On Apr 11, 1:44 pm, David Harmon <sou... [at] netcom.com> wrote:
> Elsewhere I saw someone making an argument and using a Perl program
> to support it, one in which he calls rand() in a loop 2000000 times.
> Now, in my native C, rand() typically falls apart statistically long
> before that many iterations. Is there any guarantee in Perl on the
> randomness and/or length of the period of rand?
> "perldoc -f rand" doesn't tell.


If you don't want to use a PRNG, use Crypt::Random.
smallpond [ Fr, 11 April 2008 23:04 ] [ ID #1940927 ]

Re: Quality of rand()

In article
<45f278f2-ea71-4bef-b80b-1b4856650c65 [at] m44g2000hsc.googlegroups.com>,
smallpond <smallpond [at] juno.com> wrote:
>
> If you don't want to use a PRNG, use Crypt::Random.

If the documentation on CPAN is right, that uses /dev/random. On some
Unix systems, /dev/random is a PRNG. E.g., on FreeBSD, /dev/random uses
the Yarrow PRNG. (Same for OS X).

--
--Tim Smith
Tim Smith [ Sa, 12 April 2008 04:43 ] [ ID #1941327 ]

Re: Quality of rand()

_
xhoster [at] gmail.com (xhoster [at] gmail.com) wrote on VCCCXXXVII September
MCMXCIII in <URL:news:20080411141536.213$fl [at] newsreader.com>:
~~ "Newsgroup only please, address is no longer replyable."
~~ <bad [at] example.invalid> wrote:
~~ > Elsewhere I saw someone making an argument and using a Perl program
~~ > to support it, one in which he calls rand() in a loop 2000000 times.
~~
~~ And then what? We can't critique the argument if we don't know what it is.
~~
~~ > Now, in my native C,
~~
~~ I don't have a native C. I doubt you do either. Your computer might.
~~ If your computer's native C has crappy random number generators, I suspect
~~ your perl will inherit that crappiness.
~~
~~ > rand() typically falls apart statistically long
~~ > before that many iterations.
~~
~~ All psuedorandom number generators will fall about if your statistical
~~ criteria are stringent enough and you are willing to spend enough time
~~ finding the problem. So it is kind of meaningless to say that without
~~ specifying exactly what type of falling apart you are looking for.
~~
~~ > Is there any guarantee in Perl on the
~~ > randomness and/or length of the period of rand?
~~ > "perldoc -f rand" doesn't tell.
~~
~~ I don't think perl makes any guarantees about anything.
~~
~~ By running ltrace on a simple program, I see that my perl calls drand48
~~ to get its random numbers. Therefore, it inherits the same limitations as
~~ discussed in "man drand48". I find this good enough, but if you don't
~~ there are modules for Perl that try to give you cryptographic strength
~~ random numbers. I suspect they are God-awful slow.

To see what function Perl uses for random numbers:

perl -MConfig -E 'say $Config::Config {randfunc}'

For its number of bits:

perl -MConfig -E 'say $Config::Config {randbits}'


Abigail

~~
~~ perl -le 'foreach(1..2e6) {die if $h{rand()}++}'
~~
~~ Xho
~~
--
:$:=~s:$":Just$&another$&:;$:=~s:
:Perl$"Hacker$&:;chop$:;print$:#:
Abigail [ Sa, 12 April 2008 12:55 ] [ ID #1941333 ]

Re: Quality of rand()

On 12 Apr 2008 10:55:43 GMT in comp.lang.perl.misc, Abigail
<abigail [at] abigail.be> wrote,
>To see what function Perl uses for random numbers:
>
> perl -MConfig -E 'say $Config::Config {randfunc}'
>
>For its number of bits:
>
> perl -MConfig -E 'say $Config::Config {randbits}'

Thanks, Abigail; that is very helpful.
After fixing minor syntax errors, I get:

C:\USR\perl>perl -MConfig -e "print $Config::Config {randfunc}"
rand
C:\USR\perl>perl -MConfig -e "print $Config::Config {randbits}"
15
David Harmon [ Mo, 14 April 2008 20:36 ] [ ID #1942320 ]

Re: Quality of rand()

[A complimentary Cc of this posting was sent to
Abigail
<abigail [at] abigail.be>], who wrote in article <slrng0159f.fs.abigail [at] alexandra.abigail.be>:
> To see what function Perl uses for random numbers:
>
> perl -MConfig -E 'say $Config::Config {randfunc}'
>
> For its number of bits:
>
> perl -MConfig -E 'say $Config::Config {randbits}'

Most probably, Abigail meant

perl -V:"randfunc|randbits"

One can do

perl -V:".*rand.*"

but it also will get some "detected pre-build" stuff which is not
actually used by perl; so it is better to see "randfunc|randbits"
first.

Hope this helps,
Ilya
Ilya Zakharevich [ Mo, 14 April 2008 21:39 ] [ ID #1942328 ]
Perl » comp.lang.perl.misc » Quality of rand()

Vorheriges Thema: Textview not scrolling to show full text in IRC client
Nächstes Thema: FAQ 9.7 How do I make an HTML pop-up menu with Perl?