Hello R users, Here is my question about generating random sample. How to set the random seed to recreate the same random numbers? For example, 10 random numbers is generated from N(0,1), then "runif(10)" is used.What if I want to get the same 10 random numbers when I run runif(10) again? Is it possible?I think .Random.seed should be used here. Thanks. Xiaoxi [[alternative HTML version deleted]]
Use "set.seed()" -------------------------------------- Jonathan P. Daily Technician - USGS Leetown Science Center 11649 Leetown Road Kearneysville WV, 25430 (304) 724-4480 "Is the room still a room when its empty? Does the room, the thing itself have purpose? Or do we, what's the word... imbue it." - Jubal Early, Firefly From: Xiaoxi Gao <rhelpme@hotmail.com> To: R Help <r-help@r-project.org> Date: 11/08/2010 03:59 PM Subject: [R] Random Sample Sent by: r-help-bounces@r-project.org Hello R users, Here is my question about generating random sample. How to set the random seed to recreate the same random numbers? For example, 10 random numbers is generated from N(0,1), then "runif(10)" is used.What if I want to get the same 10 random numbers when I run runif(10) again? Is it possible?I think .Random.seed should be used here. Thanks. Xiaoxi [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list 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]]
?set.seed is what you're looking for Xiaoxi Gao wrote:> Hello R users, > Here is my question about generating random sample. How to set the random seed to recreate the same random numbers? For example, 10 random numbers is generated from N(0,1), then "runif(10)" is used.What if I want to get the same 10 random numbers when I run runif(10) again? Is it possible?I think .Random.seed should be used here. > Thanks. > Xiaoxi > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi Xiaoxi, Take a look at the following:> set.seed(123) > rnorm(10)[1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499 [7] 0.46091621 -1.26506123 -0.68685285 -0.44566197> rnorm(10)[1] 1.2240818 0.3598138 0.4007715 0.1106827 -0.5558411 1.7869131 0.4978505 [8] -1.9666172 0.7013559 -0.4727914> set.seed(123) > rnorm(10)[1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499 [7] 0.46091621 -1.26506123 -0.68685285 -0.44566197 and check ?set.seed(). HTH, Jorge On Mon, Nov 8, 2010 at 3:57 PM, Xiaoxi Gao <> wrote:> > Hello R users, > Here is my question about generating random sample. How to set the random > seed to recreate the same random numbers? For example, 10 random numbers is > generated from N(0,1), then "runif(10)" is used.What if I want to get the > same 10 random numbers when I run runif(10) again? Is it possible?I think > .Random.seed should be used here. > Thanks. > Xiaoxi > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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]]
Here is an example of what I think the original poster wanted to achieve.> rnorm(10)[1] -1.2165869 -0.4698460 -0.4209811 -1.4856052 0.3765774 -1.3822470 [7] 0.2818458 0.5500957 -1.1474455 -1.2221257> x <- .Random.seed > runif(10)[1] 0.5610780 0.5911841 0.5868183 0.3833801 0.7397059 0.4973270 0.6544219 [8] 0.1257485 0.2756357 0.5672207> .Random.seed <- x > runif(10)[1] 0.5610780 0.5911841 0.5868183 0.3833801 0.7397059 0.4973270 0.6544219 [8] 0.1257485 0.2756357 0.5672207 Best, Giovanni Petris On Mon, 2010-11-08 at 16:05 -0500, Jorge Ivan Velez wrote:> Hi Xiaoxi, > > Take a look at the following: > > > set.seed(123) > > rnorm(10) > [1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499 > [7] 0.46091621 -1.26506123 -0.68685285 -0.44566197 > > rnorm(10) > [1] 1.2240818 0.3598138 0.4007715 0.1106827 -0.5558411 1.7869131 > 0.4978505 > [8] -1.9666172 0.7013559 -0.4727914 > > set.seed(123) > > rnorm(10) > [1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499 > [7] 0.46091621 -1.26506123 -0.68685285 -0.44566197 > > and check ?set.seed(). > > HTH, > Jorge > > > On Mon, Nov 8, 2010 at 3:57 PM, Xiaoxi Gao <> wrote: > > > > > Hello R users, > > Here is my question about generating random sample. How to set the random > > seed to recreate the same random numbers? For example, 10 random numbers is > > generated from N(0,1), then "runif(10)" is used.What if I want to get the > > same 10 random numbers when I run runif(10) again? Is it possible?I think > > .Random.seed should be used here. > > Thanks. > > Xiaoxi > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list > > 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 at r-project.org mailing list > 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.-- Giovanni Petris <GPetris at uark.edu> Associate Professor Department of Mathematical Sciences University of Arkansas - Fayetteville, AR 72701 Ph: (479) 575-6324, 575-8630 (fax) http://definetti.uark.edu/~gpetris/