Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>srand()</code> seeds the random number generator. Without a seed, the generator is unable to generate the numbers you are looking for. As long as one's need for random numbers is not security-critical (e.g. any sort of cryptography), common practice is to use the system time as a seed by using the <code>time()</code> function from the <code>&lt;ctime&gt;</code> library as such: <code>srand(time(0))</code>. This will seed the random number generator with the system time expressed as a Unix timestamp (i.e. the number of seconds since the date 1/1/1970). You can then use <code>rand()</code> to generate a pseudo-random number.</p> <p>Here is a quote from a <a href="https://stackoverflow.com/questions/9459035/c-rand-gives-same-number-when-running-process">duplicate question</a>:</p> <blockquote> <p>The reason is that a random number generated from the rand() function isn't actually random. It simply is a transformation. Wikipedia gives a better explanation of the meaning of pseudorandom number generator: deterministic random bit generator. Every time you call rand() it takes the seed and/or the last random number(s) generated (the C standard doesn't specify the algorithm used, though C++11 has facilities for specifying some popular algorithms), runs a mathematical operation on those numbers, and returns the result. So if the seed state is the same each time (as it is if you don't call srand with a truly random number), then you will always get the same 'random' numbers out.</p> <p>If you want to know more, you can read the following:</p> <p><a href="http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/" rel="nofollow noreferrer">http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/</a></p> <p><a href="http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/" rel="nofollow noreferrer">http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/</a></p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload