Click here to get back home

FAQ 4.10 Why aren't my random numbers random?

 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
FAQ 4.10 Why aren't my random numbers random? PerlFAQ Server 05-02-2008
Posted by PerlFAQ Server on May 2, 2008, 3:03 pm
Please log in for more thread options
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.10: Why aren't my random numbers random?

If you're using a version of Perl before 5.004, you must call "srand"
once at the start of your program to seed the random number generator.

BEGIN { srand() if $] < 5.004 }

5.004 and later automatically call "srand" at the beginning. Don't call
"srand" more than once--you make your numbers less random, rather than
more.

Computers are good at being predictable and bad at being random (despite
appearances caused by bugs in your programs :-). see the random article
in the "Far More Than You Ever Wanted To Know" collection in
http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz , courtesy of Tom Phoenix,
talks more about this. John von Neumann said, "Anyone who attempts to
generate random numbers by deterministic means is, of course, living in
a state of sin."

If you want numbers that are more random than "rand" with "srand"
provides, you should also check out the "Math::TrulyRandom" module from
CPAN. It uses the imperfections in your system's timer to generate
random numbers, but this takes quite a while. If you want a better
pseudorandom generator than comes with your operating system, look at
"Numerical Recipes in C" at http://www.nr.com/ .



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Similar ThreadsPosted
FAQ 4.10 Why aren't my random numbers random? February 8, 2005, 6:03 am
FAQ 4.10 Why aren't my random numbers random? May 6, 2005, 11:03 pm
FAQ 4.10 Why aren't my random numbers random? July 22, 2005, 4:03 am
FAQ 4.10 Why aren't my random numbers random? August 13, 2005, 4:03 pm
FAQ 4.10 Why aren't my random numbers random? October 28, 2005, 4:03 pm
FAQ 4.10 Why aren't my random numbers random? November 30, 2005, 11:03 pm
FAQ 4.10 Why aren't my random numbers random? April 9, 2006, 9:03 pm
FAQ 4.10 Why aren't my random numbers random? May 25, 2006, 9:03 pm
FAQ 4.10 Why aren't my random numbers random? August 14, 2006, 3:03 pm
FAQ 4.10 Why aren't my random numbers random? October 24, 2006, 9:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap