in reply to random number generation problem

Don't forget that when you use 'rand' you have to 'srand' to initialize the random number generator:     srand (time ^ $$ | $$ << 4); If you're really concerned about security, I would strongly recommend something better than that, but for trivial exercises, that should do just fine.

Replies are listed 'Best First'.
Re: Re: random number generation problem
by merlyn (Sage) on Apr 27, 2001 at 01:17 UTC
    Don't forget that when you use 'rand' you have to 'srand' to initialize the random number generator:
    As of 5.5 and later, you don't have to. Done for you automatically. You may want to randomize it to something a little more crypto-secure, but it will start in a spun place differently each time.

    -- Randal L. Schwartz, Perl hacker

(tye)Re: random number generation problem
by tye (Sage) on Apr 27, 2001 at 02:07 UTC

    Please don't. As others have noted, Perl often does this for you. What they haven't noted is that when Perl does, it does a better job than you did.

    For more details and suggestions of how to do it in a way that works for both old and new Perl, see Perl's auto srand() RE: a random sort of list.

            - tye (but my friends call me "Tye")
      During testing, I found it odd that rand() was producing random numbers without the use of srand(), which for the longest time was a strict requirement. Now, I would have deleted the node, but by the time I got around to doing that, the thread was growing like a weed.

      Just another one of those incredible things that changed but nobody really knew.
Re: Re: random number generation problem
by arturo (Vicar) on Apr 27, 2001 at 01:21 UTC

    A correction: perldoc -f srand says:

    In fact, it's usually not necessary to call
    `srand' at all, because if it is not called
    explicitly, it is called implicitly at the first
    use of the `rand' operator.
    

    But I note:

     However, this was not
                   the case in version of Perl before 5.004

    So there ya go.