Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Generating Random Integers

by --OutpostMir-- (Initiate)
on Jan 03, 2001 at 03:11 UTC ( #49416=perlquestion: print w/replies, xml ) Need Help??

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

Ok, I know rand can be used to get a random number, but I want to know how to generate a random integer. Why? I'm using this random integer to pull a random value out of an array.

Thanks for helping out a perl-newbie!

Replies are listed 'Best First'.
Re: Generating Random Integers
by I0 (Priest) on Jan 03, 2001 at 03:14 UTC
    $array[rand @array]
      Nice and easy solution IO. Since --OutpostMir-- mentioned he was a beginner a bit of detail might help him understand your answer.

      An array can take a non-integer subscript. It just uses the integer part. So you don't need to make it an integer. if you want to make it an integer for other reasons, you could use int() to make it an integer.

      Also the rand @array is taking @array in a scalar context. An array in scalar context returns the number of elements. So that rand @array calls rand with the number of element.

      In this case, replace $array and @array with the actual name of your array.

      =Blue
      ...you might be eaten by a grue...

        the rand @array is taking @array in a scalar context. An array in scalar context returns the number of elements

        Doesn't an array in scalar context return the index of the last element? i.e. if @array is a list of 10 values, I0's examples will be the equivalent of $array[rand 9]. rand 9 will effectively produce a random number between 0 and 8. This would mean that the last element in the array would never appear.

        Update: I'm wrong as per tilly's comment below. See my humble-pie-eating act below that one. <half-grin>
(fongsaiyuk)Re: Generating Random Integers
by fongsaiyuk (Pilgrim) on Jan 03, 2001 at 03:36 UTC
    FMTYRWTK, but depending upon your application, you might consider the use of Math::TrulyRandom, available at your local

    perl -MCPAN -e "install Math::TrulyRandom"

    Random numbers generated by computers are "mostly" random. This module does a good job of making them "really" random. :)

    I say "depending upon your application" because you may require more randomness than what is provided by the seed value you are sending srand()... :)

Re: Generating Random Integers
by $code or die (Deacon) on Jan 03, 2001 at 07:13 UTC
    In case you want to use rand for anything else and want an integer you can do the following:
    my $randominteger = int(rand 100); print $randominteger;
    The int will give you the integer part of the random number. So this example will produce a pseudorandom number between 0 and 99.

    Good Luck!
Re:(fongsaiyuk) Generating Random Integers
by fongsaiyuk (Pilgrim) on Jan 03, 2001 at 20:19 UTC
    Yet more information for successful random number generation....

    A key point is the generation of the seed value... Here is a seed value creation that I got out of a book.
    /me not a math guru to develop seed stuff :)

    srand( (time() ^ (time() % $]) ) ^ exp(length($0))**$$);

    This snippet is taken from "Perl, The Complete Reference" by Martin C. Brown, published by Osborne pp. 90-91

    Update: Why is choosing a unique seed important? rand() generates a sequence of pseudo-random numbers. This implies that the sequence of generation could possibly be predictable or duplicated. A potentially bad situation for an application that requires security.

    Intelligent seed generation helps to make that duplication or prediction difficult. That is why good seeds incorporate the use of time() and the current process id, $$.

    I've seen other functions that generate the seed value based on prime numbers and other such black magic.... :)

      It is fine to set the seed with srand(), but if you want good randomness, then don't do it quite that way because it clobbers the (probably better) seed that modern versions of Perl pick for you. Instead, add your seed into the mix:

      srand( rand(~0) ^ (time() ^ (time() % $]) ) ^ exp(length($0))**$$);

              - tye (but my friends call me "Tye")
        sweet!

        my font is a little screwy, but is that a tilde in front of the "0" in rand(~0)?

        I've not seen that before... What does it do?

      A key point is the generation of the seed value... Here is a seed value creation that I got out of a book

      perldoc -f srand will tell you more about this as well.

      In Perl's before 5.004 it was important to set a good srand value, but since then it's all been taken care of for you - one will be generated the first time you call rand.

      If you're not doing anything more advanced than "simple randomness" for nothing particularly important (i.e. not cryptography or whatever), then this should be all you ever need.

      If you're doing anything more complicated then, as others have said, look at something like Math::TrulyRandom

      (Also, don't forget to check out Simon Cozens' article on How to choose a good Perl Book. I've never actually heard of the Brown book - what's it like?)

      Tony

        In Perl's before 5.004 it was important to set a good srand value, but since then it's all been taken care of for you - one will be generated the first time you call rand.

        cool! I didn't know that. So many things to keep track off.. :)

        The book I referenced is authored by Martin C. Brown, the author of Perl Annotated Archives

        I think that the book is very good. I've read the old pink cammel several times and I still miss a few details about regex's... This book introduces them in a slightly different way that somehow has made better sense to me. :) I know that the pink is pretty old so I'm sure that the newer additions are *much* better.

        I looked on this sight for a review on the book, but didn't find one. After I get a bit farther through it I'll probably write up a review.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://49416]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2019-10-15 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?