ASA Connect

 View Only
Expand all | Collapse all

set.seed stop working in r

  • 1.  set.seed stop working in r

    Posted 12-22-2022 18:25
    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
    ------------------------------


  • 2.  RE: set.seed stop working in r

    Posted 12-23-2022 07:13

    The set.seed command is working, but not necessarily how you would expect. To see that it is working, try setting rnorm to 15, and then run your example again

    Try this out:

    > set.seed(5)
    > rnorm(15)
    [1] -0.84085548 1.38435934 -1.25549186 0.07014277 1.71144087 -0.60290798 -0.47216639
    [8] -0.63537131 -0.28577363 0.13810822 1.22763034 -0.80177945 -1.08039260 -0.15753436
    [15] -1.07176004

    Now to your example:
    > 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
    > rnorm(5)
    [1] 1.2276303 -0.8017795 -1.0803926 -0.1575344 -1.0717600

    As you can see, the sequence is set by the seed, but every time you call rnorm, it runs the next 5 numbers in the generated sequence you set. If you want to call the same 5 numbers every time, you would need to either recall the seed first every time, or create a loop to try and automate the process.

    Hope this helps!



    ------------------------------
    Bryan McComb, PhD
    Manager, Biostatistics
    ------------------------------



  • 3.  RE: set.seed stop working in r

    Posted 12-23-2022 11:23
    Hello Dr Bryan,
    Thank you so much for your response. I Think way I wrote the question was not clear. Let me clarify it like this:
    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.


    Cheers,

    ------------------------------
    Nirajan Bam
    PhD Candidate
    ------------------------------



  • 4.  RE: set.seed stop working in r

    Posted 12-23-2022 07:50
    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
    ------------------------------



  • 5.  RE: set.seed stop working in r

    Posted 12-23-2022 11:27
    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
    ------------------------------



  • 6.  RE: set.seed stop working in r

    Posted 12-23-2022 11:44
    Are you including the set seed line each time you run the simulation?

    Ed

    ------------------------------
    Edward Gracely
    ------------------------------



  • 7.  RE: set.seed stop working in r

    Posted 12-23-2022 11:47





  • 8.  RE: set.seed stop working in r

    Posted 12-23-2022 11:56
    It sounds like you are not recalling the seed every time you run your program. In my example, after the first runif(5), the next runif(5) continues on in the list, and again and again. When I reset my seed after each command, it ensured I got the same value every time. See example:

    Set seed only at beginning:
    > set.seed(5)
    > sum(runif(5))
    [1] 2.191358
    > sum(runif(5))
    [1] 3.103906
    > sum(runif(5))
    [1] 1.903968
    > sum(runif(5))
    [1] 2.874373
    > sum(runif(5))
    [1] 2.187949

    Set seed every time:
    > set.seed(5)
    > sum(runif(5))
    [1] 2.191358
    > set.seed(5)
    > sum(runif(5))
    [1] 2.191358
    > set.seed(5)
    > sum(runif(5))
    [1] 2.191358
    > set.seed(5)
    > sum(runif(5))
    [1] 2.191358
    > set.seed(5)
    > sum(runif(5))
    [1] 2.191358

    After you set the seed, your code will run through the first 750 values, and each time you call for 750 it is continuing on in the list. To ensure you are maintaining the same 750 digits, you would need to set.seed(#) each time, which tells the program to start at the beginning of the list.

    ------------------------------
    Bryan McComb, PhD
    Manager, Biostatistics
    ------------------------------



  • 9.  RE: set.seed stop working in r

    Posted 12-23-2022 11:52
    Off hand, I don't see any issue with your code, but your code isn't complete. Can you share the code that produces results that aren't replicable using set.seed?

    ------------------------------
    David Wilson
    ------------------------------



  • 10.  RE: set.seed stop working in r

    Posted 12-23-2022 12:29
    The problem is that random number seeding does not work well with parallel processing. When using package foreach with random number generation, if you need reproducible results you should also use package doRNG.

    Hope that helps!

    Kind regards, Diana Šimić

    ------------------------------
    Diana Simic
    Professor
    Zagreb,Croatia
    ------------------------------



  • 11.  RE: set.seed stop working in r

    Posted 12-23-2022 13:03
    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/NEWS

    There 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
    ------------------------------



  • 12.  RE: set.seed stop working in r

    Posted 12-24-2022 15:04
    Hello Marc,
    Thank you so much for your response. DoMC package solve my problem. I wanted to use DoRNG as Diana suggested but scared after reading the news you shared. I tried DoMC, it works fine. Thank you.



    ------------------------------
    Nirajan Bam
    PhD Candidate
    ------------------------------



  • 13.  RE: set.seed stop working in r

    Posted 12-23-2022 08:09
    I tried playing with this in R (not R Studio) and set.seed (5) works fine, but I have to do it before each time I call the random number generator,

    In your example you got random numbers twice after setting the seed once.  In the past did that give the same set of random numbers?  That would seem to be an awkward behavior.. I would want the sequences I generated to continue until I set the seed again.

    Ed

    ------------------------------
    Edward Gracely
    ------------------------------



  • 14.  RE: set.seed stop working in r

    Posted 12-23-2022 08:13
    You need to use set.seed(5) before each call to rnorm(5) to obtain the same sequence, by initializing the PRNG to the same values:

    > 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

    Note that if you used the same seed and generate 10 values:

    > set.seed(5)

    > rnorm(10)

     [1] -0.84085548  1.38435934 -1.25549186  0.07014277  1.71144087

     [6] -0.60290798 -0.47216639 -0.63537131 -0.28577363  0.13810822


    you get the same two sets of 5 values that you obtained in your post using rnorm(5) twice.

    See ?set.seed and note the first example there.

    Regards,

    Marc Schwartz



    ------------------------------
    Marc Schwartz
    ------------------------------



  • 15.  RE: set.seed stop working in r

    Posted 12-23-2022 11:29
    Hello Marc,
    Thank you so much. I think my question was not clear. Actually, I would like to generate 2 different random number set again and again. I would like to reproduce my simulations results. Thank you.

    ------------------------------
    Nirajan Bam
    PhD Candidate
    ------------------------------



  • 16.  RE: set.seed stop working in r

    Posted 12-23-2022 09:40
    Using set.seed means the subsequent sequences of randomly generated values will be reproducible. It doesn't mean that every request for random numbers will be identical after using set.seed.

    set.seed(54)
    rnorm(10)
    rnorm(10)

    means that the results of the two calls to rnorm(10) will be different but if you run this code again:

    set.seed(54)
    rnorm(10)
    rnorm(10)

    You get the same values for calls to rnorm(10) here as you did with the first run.


    ------------------------------
    David Wilson
    ------------------------------



  • 17.  RE: set.seed stop working in r

    Posted 12-23-2022 12:05
    set.seed works fine. Your second rnorm(5) gave you the next 5 random numbers. 

    > 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


    ------------------------------
    Chul (Charlie) Ahn
    Senior Distinguished Biostatistician
    Edwards Lifesciences
    ------------------------------



  • 18.  RE: set.seed stop working in r

    Posted 12-23-2022 13:50
    It's not a problem with set.seed(). To get exactly the same random numbers each time you call, e.g., rnorm(), you need to reset the seed immediately prior to the next call. Setting the seed merely initializes the random number generator for any subsequent calls to functions that generate random numbers. Calling rnorm() twice in a row after first calling set.seed() will always give you different results. You would only get the same results by using the command sequence set.seed(5), rnorm(5), set.seed(5), rnorm(5), etc.

    ------------------------------
    Kirk Cameron, PhD
    President,Statistical Scientist
    MacStat Consulting, Ltd
    ------------------------------



  • 19.  RE: set.seed stop working in r

    Posted 12-23-2022 14:15
    You should alway set the seed before run the random sampling.

    For 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

    ------------------------------
    Jin Long
    Biostatistician
    ------------------------------



  • 20.  RE: set.seed stop working in r

    Posted 12-24-2022 02:11
    Edited by Mark Lancaster 12-24-2022 02:17
    For reproducibility (say in a publication), it's also good to provide the version of R that you are using. Just in case there is a modification to the set.seed() function that changes how random values are generated. You can get this info quickly using R.Version().



    ------------------------------
    Mark Lancaster

    ------------------------------