title: R: doParallel (FORK), foreach and random number generation
body: <p>When running a <em>foreach</em> loop using the <em>doParallel</em> package in <em>R</em> and <em>FORK</em>, each worker will start off with the same random seed thus leading to duplicate 'random' numbers. </p>
  
  <pre><code>set.seed(1)
  cl &lt;- makeCluster(2, type = "FORK")
  registerDoParallel(cl)
  foreach(1:4, .combine = 'c') %dopar% {rnorm(1, mean = 0, sd = 1)}
  stopImplicitCluster()
  
  [1] -0.6264538 -0.6264538  0.1836433  0.1836433
  </code></pre>
  
  <p>What is the best way to solve this?</p>
  
  <p>Right now, I work around this problem by setting a new seed during every loop iteration, i.e.</p>
  
  <pre><code>cl &lt;- makeCluster(2, type = "FORK")
  registerDoParallel(cl)
  foreach(i = 1:4, .combine = 'c') %dopar% {
    set.seed(i)
    rnorm(1, mean = 0, sd = 1)}
  stopImplicitCluster()
  
  [1] -0.8969145 -0.9619334  0.2167549 -0.8408555
  </code></pre>