Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Can I seed srand() with results from rand()?

by no_slogan (Deacon)
on Jul 10, 2003 at 05:53 UTC ( #272898=note: print w/replies, xml ) Need Help??


in reply to Re: Can I seed srand() with results from rand()?
in thread Can I seed srand() with results from rand()?

I should have been less cavalier. The generation of random numbers is too important to be left to chance, as they say. One of the desirable features of PRNGs is that they have a provably long period before they start repeating themselves. By reseeding this way, you lose that. After a large number of iterations, you might end up in a short limit cycle of possible values for the next seed. That will depend on the details of your rand() and exactly how many times you call it per loop. So I'm afraid I have to take back my advice. Don't reseed this way, epecially if you're going to do a lot of loops.

You could use rand() with the initial seed to generate all the other seeds you need. You'd either have to know the number of loops ahead of time, or keep winding rand() forward from the initial seed.

The best solution would be to use a different random number generator that lets you get at the current seed value. There are several available. They're also easy to write -- just don't try to pick the constants yourself.

  • Comment on Re: Re: Can I seed srand() with results from rand()?

Replies are listed 'Best First'.
Re**3: Can I seed srand() with results from rand()?
by no_slogan (Deacon) on Jul 12, 2003 at 01:20 UTC
    You could use rand() with the initial seed to generate all the other seeds you need.

    ... but that's a terrible idea if your rand() doesn't hide some bits from you. Gack. Stay away.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-12-02 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?