Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

is rand() random

by thunders (Priest)
on Jun 19, 2002 at 13:46 UTC ( #175678=perlquestion: print w/replies, xml ) Need Help??

thunders has asked for the wisdom of the Perl Monks concerning the following question:

I'm a little confused abour rand() In theory, I would think the larger the number to randomise the less likely a duplicate will be picked. I notice with small values, lets say < 1000000 we get a lot of dupes printed, but as I increase the number to a huge value I still get about a dozen dupes, approximately the same that I get for randomizing a million. why isn't random more random
#!/usr/bin/perl my @numbers; push @numbers,(int rand(100000000)) for (1..1000); my %seen; for (@numbers){$seen{$_}++} for(keys %seen){ $value = $seen{$_}; print qq{$_ seen $value times\n} if ($value > 1) }

Replies are listed 'Best First'.
Re: is rand() random
by jeffenstein (Friar) on Jun 19, 2002 at 13:56 UTC

    rand() uses (IIRC) the built-in rand() function on your platform. This is a pseudo-random number generator, not true random numbers. Perhaps the rand() function is not up to snuff on your OS.

    BTW, random doesn't neccessarily mean no duplicates. The sequence 5 5 5 5 5 is just as likely as the sequence 3 2 4 1 5 or 1 2 3 4 5. It just doesn't look random to us humans, who are wired for pattern recognition.

        rand() uses (IIRC) the built-in rand() function on your platform.

      More to the point, stdlib rand() functions tend to be abysmally poor at generating even remotely random numbers (for both statistical and cryptographic definitions of "random"). Math::Random and Math::TrulyRandom are both good places to look for randomness in Perl, but coding up your own PRNG isn't terribly taxing, and a lot of fun.

      The hell with paco, vote for Erudil!

Re: is rand() random
by Abigail-II (Bishop) on Jun 19, 2002 at 15:32 UTC
    rand() uses a random number generator from your platform. This function typically returns an integer in the range 0 to some maximum RAND_MAX. That is, the output consists of a fixed number of bits. Regardless whether you are using a pseude random number generator (which will have some kind of periodicity), or some true random source. You get 15, 16, 31, 32, 48 or some other number of bits.

    All Perl is doing is do some scaling, dividing the number it got from the system by the maximum the system can return, and then multiplying it by the argument you gave to rand. Hence, the argument you give to rand has little influence on the "randomness" (there might be some roundoff errors, but they should not have much influence).

    If your program repeatedly generates output, I guess your systems C library is broken. Try translating the program to an equivalent C program and see what happens.


Re: is rand() random
by DamnDirtyApe (Curate) on Jun 19, 2002 at 13:57 UTC

    I'm afraid that I'll have to defer the real answer here to someone wiser than I, but if rand just isn't random enough for your tastes, you may be seeking Math::TrulyRandom.
    D a m n D i r t y A p e
    Home Node | Email

Re: is rand() random
by gumby (Scribe) on Jun 19, 2002 at 18:24 UTC
    'Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.'

    - John von Neumann

    I couldn't resist using that quote :-)

Re: is rand() random
by caedes (Pilgrim) on Jun 19, 2002 at 14:57 UTC
    The only way to create totally random numbers from a computer system is to use special hardware. This hardware could for instance return a number based on some scientifically random natural process such as the thermal motion of atoms and molecules (Brownian Motion). I'm pretty sure that such hardware exists and is used for scientific computing, but I kinda of doubt that your computer has one. :-)
Re: is rand() random
by smitz (Chaplain) on Jun 19, 2002 at 15:35 UTC
    For really REALLY random numbers, try:
    This generator uses radioactive decay as its seed, or some such physics gubbins.

      Actually HotBits is not a generator that uses radioactive decay as a seed, it uses the decay to actually generate bits (actually it uses pairs of timing intervals between consecutive decay events to generate either 0 or 1 bits depending on whether the first or second interval was longer). The process is slow, about 30 random bytes generated per second, and requests for HotBits (up to 2k bytes at a time) are served from an inventory of about 2 million bits that is continually topped up in the background. Of course, your primary request is processed by a Perl CGI program that fetches your bits for you.
Re: is rand() random
by thunders (Priest) on Jun 19, 2002 at 16:40 UTC
    UPDATE: This test case was done on Windows 2000 sp2. I ran the same exact progam on my Mandrake Linux 8.$something and it performed exactltly as I expected it would. what I thought was perl related appears to be as AbigailII stated a problem/limitation of my system-level random number generator.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://175678]
Approved by wil
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2021-10-19 05:07 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (76 votes). Check out past polls.