Hi Nirajan,
As Diana picked up in her reply from your expanded code snippet, as you are using the foreach package, which provides for parallel processing, the PRNG sequences can be split among the individual threads on the processors, resulting in broken and non-reproducible sequences.
Diana mentions the doRNG package on CRAN, however the status for the package has been set to "orphaned" which means that the package does not have a current maintainer. If that does not change, it will eventually be archived and possibly removed from CRAN, which would raise concerns for future use. According to the NEWS file, the package has not been touched since Jan 2020:
https://cran.r-project.org/web/packages/doRNG/NEWSThere are some additional possible solutions mentioned in this blog post:
http://www.sugarscape.net/blog/random-number-seed-in-foreach/that you might find helpful, including a suggestion that the set.seed() call should be within the loop, rather than outside of it. You will likely need to adjust your code to reflect that you are making multiple PRNG calls within the loop.
Regards,
Marc
------------------------------
Marc Schwartz
------------------------------
Original Message:
Sent: 12-23-2022 11:26
From: Nirajan Bam
Subject: set.seed stop working in r
Hello Dr Diana,
Thank you so much for your response. I think my question was not clear. I would like to generate two different set of random numbers again and again.

Above is the part of my code's screenshot. I am testing my model for parameter recovery.
In my simulations I am creating 750 data points and find parameter of each and average it to check whether it recover the parameter or not. It work fine, but if I repeat it again, my result is different. The question is how to generate same 750 different random numbers again and again to reproduce same result. Thank you.
------------------------------
Nirajan Bam
PhD Candidate
Original Message:
Sent: 12-23-2022 07:50
From: Diana Simic
Subject: set.seed stop working in r
set.seed() in your RStudio works well. Check this example:
> set.seed(5)> rnorm(5)[1] -0.84085548 1.38435934 -1.25549186 0.07014277 1.71144087> set.seed(5)> rnorm(5)[1] -0.84085548 1.38435934 -1.25549186 0.07014277 1.71144087> rnorm(5)[1] -0.6029080 -0.4721664 -0.6353713 -0.2857736 0.1381082
Call to rnorm() changes the seed. That is why the second call to rnorm() in your example has different results that the first call. If you execute set.seed(5) before the second call - results will be the same as after the first call. Your sequence: set.seed(), rnorm(), rnorm() gives the same results in my RStudio as in yours. That is the intended behavior.
Kind regards, Diana Šimić
------------------------------
Diana Simic
Professor
Zagreb,Croatia
Original Message:
Sent: 12-22-2022 18:24
From: Nirajan Bam
Subject: set.seed stop working in r
Dear all,
set.seed is stop working on my Rstudio. It is giving me different random numbers every time even after using set.seed like this:
Is anyone know how to fix this issue? Thanking you.
Cheers,
Nirajan Bam
------------------------------
Nirajan Bam
PhD Candidate
------------------------------