Andras Farkas
2012-Sep-01 09:29 UTC
[R] help on setting boundaries for generating random numbers
Dear All, is there a way to set low and high limits to a simulation with rlnorm()? as an example: a <-rlnorm(500,0.7,1) I get the summary of Min. 1st Qu. Median Mean 3rd Qu. Max. 0.1175 1.0590 2.1270 3.4870 4.0260 45.3800 I would like to set limits so that the simulated values minimum would be greater then 0.5 and maximum of less than 30. If during simulation a value outside of the limits would be simulated, then I would like R to "throw that value out" and go back to generate another random number instead that would fit the limits criteria. Apreciate all the help on this, Sincerely, Andras [[alternative HTML version deleted]]
Hi, May be this might help you. set.seed(1) ?a <-rlnorm(500,0.7,1) ?a1<-a[a>30|a<0.5] set.seed(2) a2 <-rlnorm(500,0.7,1) a3<-a2[a2>30|a2<0.5] a4<-a2[!a2%in%a3] summary(a4) #?? Min. 1st Qu.? Median??? Mean 3rd Qu.??? Max. # 0.5105? 1.2500? 2.2990? 3.6850? 4.7140 25.3600 a[a%in%a1]<-sample(a4,length(a[a%in%a1]),replace=FALSE) summary(a) #?? Min. 1st Qu.? Median??? Mean 3rd Qu.??? Max. # 0.5044? 1.2900? 2.1310? 3.5320? 4.1510 28.4800 ?a[a>30|a<0.5] #numeric(0) A.K. ----- Original Message ----- From: Andras Farkas <motyocska at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Saturday, September 1, 2012 5:29 AM Subject: [R] help on setting boundaries for generating random numbers Dear All, ? is there a way to set low and high limits to a simulation with rlnorm()? ? as an example: ?a <-rlnorm(500,0.7,1) ? ? I get the summary of ? Min. 1st Qu.? Median? ? Mean 3rd Qu.? ? Max. 0.1175? 1.0590? 2.1270? 3.4870? 4.0260 45.3800? I would like to set limits so that the simulated values minimum would be greater then 0.5 and maximum of less than 30. If during simulation a value?outside of the limits would be simulated, then I would like R to "throw that value out" and go back to generate another random number instead that would fit the limits criteria. ? Apreciate all the help on this, ? Sincerely, ? Andras ??? [[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.
Petr Savicky
2012-Sep-01 17:20 UTC
[R] help on setting boundaries for generating random numbers
On Sat, Sep 01, 2012 at 02:29:40AM -0700, Andras Farkas wrote:> Dear All, > ? > is there a way to set low and high limits to a simulation with rlnorm()? > ? > as an example: > ?a <-rlnorm(500,0.7,1) > ? > ? > I get the summary of > ? > Min. 1st Qu. Median Mean 3rd Qu. Max.0.1175 1.0590 2.1270 3.4870 4.0260 45.3800> > I would like to set limits so that the simulated values minimum would be greater then 0.5 and maximum of less than 30. If during simulation a value?outside of the limits would be simulated, then I would like R to "throw that value out" and go back to generate another random number instead that would fit the limits criteria.Hi. If you want to generate one number at a time, try this while (1) { a <- rlnorm(1, 0.7, 1) if (0.5 < a && a < 30) break } If you want to generate a vector and avoid a loop over its components, try something like the following n <- 500 while (1) { a <- rlnorm(2*n, 0.7, 1) a <- a[0.5 < a & a < 30] # only one & here if (length(a) >= n) break } a <- a[1:n] Hope this helps. Petr Savicky.