title: Distinct rand() sequences yielding the same results in an expression
body: <p>Ok, this is a really weird one.</p>
  
  <p>I have an MPI program, where each process has to generate random numbers in a fixed range (the range is read from file). What happens is that even though I seed each process with a different value, and the numbers generated by <code>rand()</code> are different in each process, the expression to generate the random numbers still yields the same sequence between them.</p>
  
  <p>Here's all relevant code:</p>
  
  <pre><code>// 'rank' will be unique for each process
  int rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
  // seed the RNG with a different value for each process
  srand(time(NULL) + rank);
  // print some random numbers to see if we get a unique sequence in each process
  // 'log' is a uniquely named file, each process has its own
  log &lt;&lt; rand() &lt;&lt; " " &lt;&lt; rand() &lt;&lt; " " &lt;&lt; rand() &lt;&lt; std::endl;
  
  // do boring deterministic stuff
  
  while (true)
  {
      // waitTimeMin and waitTimeMax are integers, Max is always greater than Min
      waitSecs = waitTimeMin + rand() % (waitTimeMax - waitTimeMin);
      log &lt;&lt; "waiting " &lt;&lt; waitSecs &lt;&lt; " seconds" &lt;&lt; std::endl;
      sleep(waitSecs);
      // do more boring deterministic stuff
  }
  </code></pre>
  
  <p>Here's the output of each process, with 3 processes generating numbers in the range [1,9].</p>
  
  <p>process 1:</p>
  
  <pre><code>15190 28284 3149
  waiting 6 seconds
  waiting 8 seconds
  waiting 9 seconds
  waiting 4 seconds
  </code></pre>
  
  <p>process 2:</p>
  
  <pre><code>286 6264 3153
  waiting 6 seconds
  waiting 8 seconds
  waiting 9 seconds
  waiting 4 seconds
  </code></pre>
  
  <p>process 3:</p>
  
  <pre><code>18151 17013 3156
  waiting 6 seconds
  waiting 8 seconds
  waiting 9 seconds
  waiting 4 seconds
  </code></pre>
  
  <p>So while <code>rand()</code> clearly generates different numbers, the expression to calculate <code>waitSecs</code> still evaluates to the same sequence on all processes. What's even weirder: if I run the program with the same parameteres again, only the first 3 random numbers will change, the rest of the "random" sequence will be exactly the same in each run! Changing the range of numbers will obviously produce a different result from this one, but the same parameters always yield the same sequence, between processes <strong>and</strong> between executions: except for the first 3 numbers.</p>
  
  <p>Just what the hell is going on here?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> So just to see if it's the simplistic random generation and/or low range, I replaced the random generation with this line:</p>
  
  <pre><code>waitSecs = waitTimeMin + (int)((double)rand() / ((double)RAND_MAX + 1) * (waitTimeMax - waitTimeMin));
  </code></pre>
  
  <p>And started generating numbers in the range [1,99]. Here's the result:</p>
  
  <p>process 1:</p>
  
  <pre><code>7833 3798 10977
  waiting 1 seconds
  waiting 20 seconds
  waiting 58 seconds
  waiting 35 seconds
  waiting 82 seconds
  waiting 18 seconds
  </code></pre>
  
  <p>process 2:</p>
  
  <pre><code>25697 14547 10980
  waiting 1 seconds
  waiting 20 seconds
  waiting 58 seconds
  waiting 35 seconds
  waiting 82 seconds
  waiting 18 seconds
  </code></pre>
  
  <p>process 3:</p>
  
  <pre><code>10794 25295 10984
  waiting 1 seconds
  waiting 20 seconds
  waiting 58 seconds
  waiting 35 seconds
  waiting 82 seconds
  waiting 18 seconds
  </code></pre>
  
  <p>Same thing. Can this still be just <code>rand()</code> being really bad?</p>
  
  <p>EDIT2: Same thing when generating numbers from 1 to 10000.</p>