[R] Getting different results with set.seed()

Henrik Bengtsson henr|k@bengt@@on @end|ng |rom gm@||@com
Thu Aug 19 15:24:49 CEST 2021


You need to use %dorng% from the doRNG package instead of %dopar% when
parallelizing with foreach::foreach() to get reproducible random
numbers.  See also
https://www.jottr.org/2020/09/22/push-for-statical-sound-rng/.

/Henrik

On Thu, Aug 19, 2021 at 1:38 PM Eric Berger <ericjberger using gmail.com> wrote:
>
> In that case, another interesting test would be to check whether the
> problem exists when you don't use doParallel().
>
>
> On Thu, Aug 19, 2021 at 2:28 PM Shah Alam <dr.alamsolangi using gmail.com> wrote:
> >
> > Dear All,
> >
> > Thanks a lot for your valuable suggestions. I am going to implement one by
> > one.
> >
> > Jan:
> >
> > Yes, I am using the "doParallel" package for parallelization. I will let
> > you know the results after implementing all the given suggestions.
> >
> > Best regards,
> > Shah
> >
> >
> >
> > On Thu, 19 Aug 2021 at 11:57, Jan van der Laan <rhelp using eoos.dds.nl> wrote:
> >
> > >
> > >
> > > What you could also try is check if the self coded functions use the
> > > random generator when defining them:
> > >
> > > starting_seed <- .Random.seed
> > >
> > > Step 1. Self-coded functions (these functions generate random numbers as
> > > well)
> > >
> > > # check if functions have modified the seed:
> > > all.equal(starting_seed, .Random.seed)
> > >
> > > Step 2: set.seed (123)
> > >
> > >
> > >
> > > What has also happened to me is that some of the functions I called had
> > > their own random number generator independent of that of R. For example
> > > using one in C/C++.
> > >
> > > Do your functions do stuff in parallel? For example using the parallel
> > > or snow package? In that case you also have to set the seed in the
> > > parallel workers.
> > >
> > > Best,
> > > Jan
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > On 19-08-2021 11:25, PIKAL Petr wrote:
> > > > Hi
> > > >
> > > > Did you try different order?
> > > >
> > > > Step 2: set.seed (123)
> > > >
> > > > Step 1. Self-coded functions (these functions generate random numbers as
> > > well)
> > > >
> > > > Step 3: Call those functions.
> > > >
> > > > Step 4: model results.
> > > >
> > > > Cheers
> > > > Petr.
> > > >
> > > > And BTW, do not use HTML formating, it could cause problems in text only
> > > list.
> > > >
> > > >
> > > > From: Shah Alam <dr.alamsolangi using gmail.com>
> > > > Sent: Thursday, August 19, 2021 10:10 AM
> > > > To: PIKAL Petr <petr.pikal using precheza.cz>
> > > > Cc: r-help mailing list <r-help using r-project.org>
> > > > Subject: Re: [R] Getting different results with set.seed()
> > > >
> > > > Dear Petr,
> > > >
> > > > It is more than 2000 lines of code with a lot of functions and data
> > > inputs. I
> > > > am not sure whether it would be useful to upload it. However, you are
> > > > absolutely right. I used
> > > >
> > > > Step 1. Self-coded functions (these functions generate random numbers as
> > > well)
> > > >
> > > > Step 2: set.seed (123)
> > > >
> > > > Step 3: Call those functions.
> > > >
> > > > Step 4: model results.
> > > >
> > > > I close the R session and run the code from step 1. I get different
> > > results
> > > > for the same set of values for parameters.
> > > >
> > > > Best regards,
> > > > Shah
> > > >
> > > >
> > > >
> > > >
> > > > On Thu, 19 Aug 2021 at 09:56, PIKAL Petr <mailto:petr.pikal using precheza.cz>
> > > > wrote:
> > > > Hi
> > > >
> > > > Please provide at least your code preferably with some data to reproduce
> > > > this behaviour. I wonder if anybody could help you without such
> > > information.
> > > >
> > > > My wild guess is that you used
> > > >
> > > > set.seed(1234)
> > > >
> > > > some code
> > > >
> > > > the code used again
> > > >
> > > > in which case you have to expect different results.
> > > >
> > > > Cheers
> > > > Petr
> > > >
> > > >> -----Original Message-----
> > > >> From: R-help <mailto:r-help-bounces using r-project.org> On Behalf Of Shah
> > > Alam
> > > >> Sent: Thursday, August 19, 2021 9:46 AM
> > > >> To: r-help mailing list <mailto:r-help using r-project.org>
> > > >> Subject: [R] Getting different results with set.seed()
> > > >>
> > > >> Dear All,
> > > >>
> > > >> I was using set.seed to reproduce the same results for the discrete
> > > event
> > > >> simulation model. I have 12 unknown parameters for optimization (just a
> > > >> little background). I got a good fit of parameter combinations. However,
> > > >> when I use those parameters combinations again in the model. I am
> > > getting
> > > >> different results.
> > > >>
> > > >> Is there any problem with the set.seed. I assume the set.seed should
> > > >> produce the same results.
> > > >>
> > > >> I used set.seed(1234).
> > > >>
> > > >> Best regards,
> > > >> Shah
> > > >>
> > > >>        [[alternative HTML version deleted]]
> > > >>
> > > >> ______________________________________________
> > > >> mailto:R-help using r-project.org mailing list -- To UNSUBSCRIBE and more,
> > > see
> > > >> https://stat.ethz.ch/mailman/listinfo/r-help
> > > >> PLEASE do read the posting guide http://www.R-project.org/posting-
> > > >> guide.html
> > > >> and provide commented, minimal, self-contained, reproducible code.
> > > >>
> > > >> ______________________________________________
> > > >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > > >> https://stat.ethz.ch/mailman/listinfo/r-help
> > > >> PLEASE do read the posting guide
> > > http://www.R-project.org/posting-guide.html
> > > >> and provide commented, minimal, self-contained, reproducible code.
> > >
> > > ______________________________________________
> > > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > > http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list