Quality of rand()

Quality of rand()

am 11.04.2008 19:44:25 von David Harmon

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.

Re: Quality of rand()

am 11.04.2008 20:15:34 von xhoster

"Newsgroup only please, address is no longer replyable."
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.

Re: Quality of rand()

am 11.04.2008 23:04:19 von smallpond

On Apr 11, 1:44 pm, David Harmon 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.

Re: Quality of rand()

am 12.04.2008 04:43:00 von Tim Smith

In article
<45f278f2-ea71-4bef-b80b-1b4856650c65@m44g2000hsc.googlegroups.com>,
smallpond 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

Re: Quality of rand()

am 12.04.2008 12:55:43 von Abigail

_
xhoster@gmail.com (xhoster@gmail.com) wrote on VCCCXXXVII September
MCMXCIII in :
~~ "Newsgroup only please, address is no longer replyable."
~~ 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$:#:

Re: Quality of rand()

am 14.04.2008 20:36:47 von David Harmon

On 12 Apr 2008 10:55:43 GMT in comp.lang.perl.misc, Abigail
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

Re: Quality of rand()

am 14.04.2008 21:39:43 von Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Abigail
], who wrote in article :
> 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