From hb at stat.berkeley.edu  Wed Feb 13 20:23:48 2008
From: hb at stat.berkeley.edu (Henrik Bengtsson)
Date: Wed, 13 Feb 2008 11:23:48 -0800
Subject: [Rd] Best way to reset random seed when using set.seed() in a
	function?
In-Reply-To: <47B33FE9.2090301@bank-banque-canada.ca>
References: <59d7961d0802130857l7b4abcfcu891c066bcbe3d21a@mail.gmail.com>
	<47B33FE9.2090301@bank-banque-canada.ca>
Message-ID: <59d7961d0802131123m6bd8fb9fx2efbf4c709a397be@mail.gmail.com>

On Feb 13, 2008 11:07 AM, Paul Gilbert <pgilbert at bank-banque-canada.ca> wrote:
> I think in general it is a good idea to use  on.exit()  to reset the
> seed if you mess with it. Otherwise you can generate unintended
> consequences for other programs. Something like:
>     old.seed <- setRNG(kind = "default", seed = test.seed, normal.kind =
> "default")
>     on.exit(setRNG(old.seed))

Yes, agree; on.exit() should always be used when you want to cleanup
after yourself.  Much better.

>
> (BTW, setRNG is a simple little utility in the setRNG package, to help
> set and save some of the other things you need besides the seed, if you
> are going to try to reproduce results.)

Thanks for this note.  This seems much more natural to work with than
'.Random.seed'.

/Henrik


>
> Paul
>
>
> Henrik Bengtsson wrote:
> > Hi,
> >
> > this is related to a question just raised on Bioconductor where one
> > function sets the random seed internally but never resets it, which
> > results in enforced down streams random samples being deterministic.
> >
> > What is the best way to reset the random seed when you use set.seed()
> > within a function?  Is it still to re-assign '.Random.seed' in the
> > global environment as following example illustrates?  I'm not too kind
> > of having function modifying the global environment, if not really
> > necessary.
> >
> > foo <- function(n) {
> >   # Pop random seed
> >   if (!exists(".Random.seed", mode="numeric"))
> >     sample(NA);
> >   oldSeed <- .Random.seed;
> >
> >   # Fixed seed to get reproducible samples here
> >   set.seed(0xbeef);
> >   x <- sample(5);
> >
> >   # Proof of concept: 'x' should be the same all the time
> >   stopifnot(identical( x, as.integer(c(4,2,5,1,3)) ));
> >
> >   # Push random seed to old state
> >   assign(".Random.seed", oldSeed, envir=globalenv())
> >
> >   # Continue as nothing happened
> >   sample(n);
> > }
> >
> >
> >> foo(5)
> >>
> > [1] 4 2 3 5 1
> >
> >> foo(5)
> >>
> > [1] 4 2 3 1 5
> >
> >> foo(5)
> >>
> > [1] 5 3 1 4 2
> >
> >> foo(5)
> >>
> > [1] 5 3 2 4 1
> >
> >> foo(5)
> >>
> >
> > Is this the way to do it?
> >
> > Thanks
> >
> > Henrik
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> ====================================================================================
>
> La version fran?aise suit le texte anglais.
>
> ------------------------------------------------------------------------------------
>
> This email may contain privileged and/or confidential information, and the Bank of
> Canada does not waive any related rights. Any distribution, use, or copying of this
> email or the information it contains by other than the intended recipient is
> unauthorized. If you received this email in error please delete it immediately from
> your system and notify the sender promptly by email that you have done so.
>
> ------------------------------------------------------------------------------------
>
> Le pr?sent courriel peut contenir de l'information privil?gi?e ou confidentielle.
> La Banque du Canada ne renonce pas aux droits qui s'y rapportent. Toute diffusion,
> utilisation ou copie de ce courriel ou des renseignements qu'il contient par une
> personne autre que le ou les destinataires d?sign?s est interdite. Si vous recevez
> ce courriel par erreur, veuillez le supprimer imm?diatement et envoyer sans d?lai ?
> l'exp?diteur un message ?lectronique pour l'aviser que vous avez ?limin? de votre
> ordinateur toute copie du courriel re?u.
>



[Rd] Best way to reset random seed when using set.seed() in a function?
Henrik Bengtsson