Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re:(fongsaiyuk) Generating Random Integers

by fongsaiyuk (Pilgrim)
on Jan 03, 2001 at 20:19 UTC ( #49533=note: print w/replies, xml ) Need Help??


in reply to Generating Random Integers

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.... :)

Replies are listed 'Best First'.
(tye)Re: Generating Random Integers
by tye (Sage) on Jan 03, 2001 at 21:08 UTC

    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?

        Yes, tilde zero (~0) is a bit-wise "not" of 0, that is, an integer with all bits set. So it gets interpretted as an unsigned value and tells rand() to give back a random pattern of bits (a random number between 0 and (~0)-1, inclusive).

        Gee, I guess I should be using rand(1+~0) from now on. Oh well, that would only be a minor improvement. (:

                - tye (but my friends call me "Tye")
Re: Re:(fongsaiyuk) Generating Random Integers
by salvadors (Pilgrim) on Jan 03, 2001 at 20:31 UTC
    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: note [id://49533]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2019-08-22 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?