Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The confusion here is about how pseudorandom number generators work.</p> <p>Pseudorandom number generators like C's <code>rand</code> work by having a number representing the current 'state'. Every time the <code>rand</code> function is called, some deterministic computations are done on the 'state' number to produce the next 'state' number. Thus, if the generator is given the same input (the same 'state'), it will produce the same output.</p> <p>So, when you seed the generator with <code>srand(74)</code>, it will always generate the same string of numbers, every time. When you seed the generator with <code>srand(75)</code>, it will generate a different string of numbers, etc.</p> <p>The common way to ensure different output each time is to always provide a different seed, usually done by seeding the generator with the current time in seconds/milliseconds, e.g. <code>srand(time(NULL))</code>.</p> <p>EDIT: Here is a Python session demonstrating this behavior. It is entirely expected.</p> <pre><code>&gt;&gt;&gt; import random </code></pre> <p>If we seed the generator with the same number, it will always output the same sequence:</p> <pre><code>&gt;&gt;&gt; random.seed(500) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [80, 95, 58, 25, 76, 37, 80, 34, 57, 79, 1, 33, 40, 29, 92, 6, 45, 31, 13, 11] &gt;&gt;&gt; random.seed(500) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [80, 95, 58, 25, 76, 37, 80, 34, 57, 79, 1, 33, 40, 29, 92, 6, 45, 31, 13, 11] &gt;&gt;&gt; random.seed(500) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [80, 95, 58, 25, 76, 37, 80, 34, 57, 79, 1, 33, 40, 29, 92, 6, 45, 31, 13, 11] </code></pre> <p>If we give it a different seed, even a slightly different one, the numbers will be totally different from the old seed, yet still the same if the same (new) seed is used:</p> <pre><code>&gt;&gt;&gt; random.seed(501) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [64, 63, 24, 81, 33, 36, 72, 35, 95, 46, 37, 2, 76, 21, 46, 68, 47, 96, 39, 36] &gt;&gt;&gt; random.seed(501) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [64, 63, 24, 81, 33, 36, 72, 35, 95, 46, 37, 2, 76, 21, 46, 68, 47, 96, 39, 36] &gt;&gt;&gt; random.seed(501) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [64, 63, 24, 81, 33, 36, 72, 35, 95, 46, 37, 2, 76, 21, 46, 68, 47, 96, 39, 36] </code></pre> <p>How do we make our program have different behavior each time? If we supply the same seed, it will always behave the same. We can use the <code>time.time()</code> function, which will yield a different number each time we call it:</p> <pre><code>&gt;&gt;&gt; import time &gt;&gt;&gt; time.time() 1347917648.783 &gt;&gt;&gt; time.time() 1347917649.734 &gt;&gt;&gt; time.time() 1347917650.835 </code></pre> <p>So if we keep re-seeding it with a call to <code>time.time()</code>, we will get a different sequence of numbers each time, because the seed is different each time:</p> <pre><code>&gt;&gt;&gt; random.seed(time.time()) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [60, 75, 60, 26, 19, 70, 12, 87, 58, 2, 79, 74, 1, 79, 4, 39, 62, 20, 28, 19] &gt;&gt;&gt; random.seed(time.time()) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [98, 45, 85, 1, 67, 25, 30, 88, 17, 93, 44, 17, 94, 23, 98, 32, 35, 90, 56, 35] &gt;&gt;&gt; random.seed(time.time()) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [44, 17, 10, 98, 18, 6, 17, 15, 60, 83, 73, 67, 18, 2, 40, 76, 71, 63, 92, 5] </code></pre> <p>Of course, even better than constantly re-seeding it is to seed it once and keep going from there:</p> <pre><code>&gt;&gt;&gt; random.seed(time.time()) &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [94, 80, 63, 66, 31, 94, 74, 15, 20, 29, 76, 90, 50, 84, 43, 79, 50, 18, 58, 15] &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [30, 53, 75, 19, 35, 11, 73, 88, 3, 67, 55, 43, 37, 91, 66, 0, 9, 4, 41, 49] &gt;&gt;&gt; [random.randint(0, 100) for _ in xrange(20)] [69, 7, 25, 68, 39, 57, 72, 51, 33, 93, 81, 89, 44, 61, 78, 77, 43, 10, 33, 8] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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