[R] random value generation with added constraint.

Tóth Zoltán tz at looper.hu
Fri Aug 5 09:51:27 CEST 2011


Hi,

If I understand correctly, you can just simply keep generating norm
values till you don't get the 100 appropriate one. I am pretty new to
R, but here you go with a function that does this:


rnorm25 <- function(mean,sd,X){
   i = 0;
   ret = c()
   while ( i < 100 ){
     r = rnorm(1,mean,sd)
     if ( r >= X & r <= X + 25 ){
         ret = c(ret,r)
         i = i + 1
     }
   }
   print(ret)
}

# test
rnorm25(10,20,17)

Cheers,
zoltanctoth

On Thu, Aug 4, 2011 at 9:03 AM, Vijayan Padmanabhan
<padmanabhan.vijayan at gmail.com> wrote:
> Hi
> I am looking at generating a random dataset of say 100 values fitting in a
> normal distribution of a given mean and SD, I am aware of rnorm
> function. However i am trying to build into this function one added
> constraint that all the random value generated should also obey the
> constraint that they only take values between say X to X+25
> How do i do this in R?
> Any help would be highly appreciated,.
> Thanks
> Vijayan Padmanabhan
>
>        [[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.
>



More information about the R-help mailing list