Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: srand producing the same sequence of random numbers

by pemungkah (Priest)
on Oct 12, 2010 at 21:04 UTC ( [id://864945]=note: print w/replies, xml ) Need Help??


in reply to Re: srand producing the same sequence of random numbers
in thread srand producing the same sequence of random numbers

This is the answer the OP is looking for.

Seeding a random number generator means giving it a number to start from - all "random number generators" that are not hooked to an actual source of entropy (like atomic decay, thermal noise, or the like) are simply doing calculations that return a sequence of numbers whose distribution looks random "enough". Knuth spends an entire volume of different definitions of "enough"; it's a very deep (and interesting) subject. (Most random number generators use a "linear congruential" calculation that essentially uses two largish numbers that are relatively prime to one another; the seed is multiplied by one, and then the product is reduced modulo the other. This has the advantage that it's a fairly fast operation (one multiply and one divide), and yields fairly good results.)

Seed functions exist to allow you to reproduce a given random sequence; this can be very useful when you're trying to test a module which contains a random component, as the behavior is still "random" (that is driven by the output of the rand() function) but at the same time completely predictable (since the sequence of numbers coming from rand() will be exactly the same every time for the same seed).

  • Comment on Re^2: srand producing the same sequence of random numbers

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-16 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found