Click here to get back home

Quality of rand()

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Quality of rand() David Harmon 04-11-2008
Posted by David Harmon on April 11, 2008, 1:44 pm
Please log in for more thread options
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.

Posted by xhoster on April 11, 2008, 2:15 pm
Please log in for more thread options
"Newsgroup only please, address is no longer replyable."
> 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++}'

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.

Posted by David Harmon on April 14, 2008, 2:36 pm
Please log in for more thread options
On 12 Apr 2008 10:55:43 GMT in comp.lang.perl.misc, Abigail
>To see what function Perl uses for random numbers:
>
> perl -MConfig -E 'say $Config::Config '
>
>For its number of bits:
>
> perl -MConfig -E 'say $Config::Config '

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

C:\USR\perl>perl -MConfig -e "print $Config::Config "
rand
C:\USR\perl>perl -MConfig -e "print $Config::Config "
15

Posted by Ilya Zakharevich on April 14, 2008, 3:39 pm
Please log in for more thread options
[A complimentary Cc of this posting was sent to
Abigail
> To see what function Perl uses for random numbers:
>
> perl -MConfig -E 'say $Config::Config '
>
> For its number of bits:
>
> perl -MConfig -E 'say $Config::Config '

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

Posted by smallpond on April 11, 2008, 5:04 pm
Please log in for more thread options
> 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.

Similar ThreadsPosted
Re: The Importance of Terminology's Quality May 10, 2008, 9:33 am
Re: The Importance of Terminology's Quality May 29, 2008, 8:31 pm
Re: The Importance of Terminology's Quality June 1, 2008, 4:24 am
Fast and high-quality Web Site Creation December 12, 2004, 3:20 am
Accessing TestDirector for Quality Center OTA API using Perl May 3, 2006, 3:53 pm
Xah's Edu Corner: Examples of Quality Technical Writing December 5, 2005, 8:55 pm
rand() question September 14, 2004, 9:58 pm
rand() with less decimals. September 8, 2006, 11:58 am
Boost your business with Quality Web & Design Services at Bargain Prices! January 17, 2008, 9:53 am
Using rand(), how to I avoid repeats? July 21, 2004, 12:51 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap