Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6887467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:53:18+00:00 2026-05-27T05:53:18+00:00

I need to do some simulations and for debugging purposes I want to use

  • 0

I need to do some simulations and for debugging purposes I want to use set.seed to get the same result. Here is the example of what I am trying to do:

library(foreach)
library(doMC)
registerDoMC(2)

set.seed(123)
a <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
set.seed(123)
b <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}

Objects a and b should be identical, i.e. sum(abs(a-b)) should be zero, but this is not the case. I am doing something wrong, or have I stumbled on to some feature?

I am able to reproduce this on two different systems with R 2.13 and R 2.14

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:53:18+00:00Added an answer on May 27, 2026 at 5:53 am

    My default answer used to be “well then don’t do that” (using foreach) as the snow package does this (reliably!) for you.

    But as @Spacedman points out, Renaud’s new doRNG is what you are looking for if you want to remain with the doFoo / foreach family.

    The real key though is a clusterApply-style call to get the seeds set on all nodes. And in a fashion that coordinated across streams. Oh, and did I mention that snow by Tierney, Rossini, Li and Sevcikova has been doing this for you for almost a decade?

    Edit: And while you didn’t ask about snow, for completeness here is an example from the command-line:

    edd@max:~$ r -lsnow -e'cl <- makeSOCKcluster(c("localhost","localhost"));\
             clusterSetupRNG(cl);\
             print(do.call("rbind", clusterApply(cl, 1:4, \
                                                 function(x) { stats::rnorm(1) } )))'
    Loading required package: utils
    Loading required package: utils
    Loading required package: rlecuyer
               [,1]
    [1,] -1.1406340
    [2,]  0.7049582
    [3,] -0.4981589
    [4,]  0.4821092
    edd@max:~$ r -lsnow -e'cl <- makeSOCKcluster(c("localhost","localhost"));\
             clusterSetupRNG(cl);\
             print(do.call("rbind", clusterApply(cl, 1:4, \
                                                 function(x) { stats::rnorm(1) } )))'
    Loading required package: utils
    Loading required package: utils
    Loading required package: rlecuyer
               [,1]
    [1,] -1.1406340
    [2,]  0.7049582
    [3,] -0.4981589
    [4,]  0.4821092
    edd@max:~$ 
    

    Edit: And for completeness, here is your example combined with what is in the docs for doRNG

    > library(foreach)
    R> library(doMC)
    Loading required package: multicore
    
    Attaching package: ‘multicore’
    
    The following object(s) are masked from ‘package:parallel’:
    
        mclapply, mcparallel, pvec
    
    R> registerDoMC(2)
    R> library(doRNG)
    R> set.seed(123)
    R> a <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
    R> set.seed(123)
    R> b <- foreach(i=1:2,.combine=cbind) %dopar% {rnorm(5)}
    R> identical(a,b)
    [1] FALSE                     ## ie standard approach not reproducible
    R>
    R> seed <- doRNGseed()
    R> a <- foreach(i=1:2,combine=cbind) %dorng% { rnorm(5) }
    R> b <- foreach(i=1:2,combine=cbind) %dorng% { rnorm(5) }
    R> doRNGseed(seed)
    R> a1 <- foreach(i=1:2,combine=cbind) %dorng% { rnorm(5) }
    R> b1 <- foreach(i=1:2,combine=cbind) %dorng% { rnorm(5) }
    R> identical(a,a1) && identical(b,b1)
    [1] TRUE                      ## all is well now with doRNGseed()
    R> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help about with Memcache. I have created a class and want to
Need some best practice advice here... Navigation based application. Root view is a UITableView
I'm currently porting some windows code and trying to make it available for use
I have to use C and Fortran together to do some simulations. In their
I want do do some flocking simulation, as described here . For this I
I'm trying to write a driving simulation program and I need some help on
I'm looking to do some physics simulations and I need fast rendering in Java.
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some guidance figuring out what went wrong. I've been using mysql, phpmyadmin for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.