Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

(tye)Re: Generating Random Integers

by tye (Sage)
on Jan 03, 2001 at 21:08 UTC ( #49556=note: print w/replies, xml ) Need Help??


in reply to Re:(fongsaiyuk) Generating Random Integers
in thread Generating Random Integers

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")

Replies are listed 'Best First'.
(fongsaiyuk)Re: (tye)Re: Generating Random Integers
by fongsaiyuk (Pilgrim) on Jan 03, 2001 at 21:29 UTC
    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")

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2019-10-20 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?