title: How to set seeds when using parallel package in R
body: <p>I am currently using the <code>parallel</code> package in R and I am trying to make by work reproducible by setting seeds. </p>
  
  <p>However, if you set the seed before creating the cluster and performing the tasks you want in parallel, for some reason, it doesn't make it reproducible. I think I need to set the seed for each core when I make the cluster. </p>
  
  <p>I have made a small example here to illustrate my problem:</p>
  
  <pre><code>library(parallel)
  
  # function to generate 2 uniform random numbers
  runif_parallel &lt;- function() {
    # make cluster of two cores
    cl &lt;- parallel::makeCluster(2)
  
    # sample uniform random numbers
    samples &lt;- parallel::parLapplyLB(cl, X = 1:2, fun = function(i) runif(1))
  
    # close cluster
    parallel::stopCluster(cl)
  
    return(unlist(samples))
  }
  
  set.seed(41)
  test1 &lt;- runif_parallel()
  
  set.seed(41)
  test2 &lt;- runif_parallel()
  
  # they should be the same since they have the same seed
  identical(test1, test2)
  </code></pre>
  
  <p>In this example, the <code>test1</code> and <code>test2</code> should be the same, as they have the same seed, but they return different results. </p>
  
  <p>Can I get some help with where I'm going wrong please?</p>
  
  <p>Note that I've written this example the way I have to mimic how I'm using it right now - there are probably cleaner ways to generate two random uniform numbers in parallel. </p>