I have modified my example to make it more convincing! See at end. On 10-Mar-09 23:39:17, Ted Harding wrote: > On 10-Mar-09 23:01:45, g...@ucalgary.ca wrote: >> Please forget the last email I sent with the same subject. >> ================= >> I would like to generate pseudo-random numbers between two numbers >> using R, up to a given distribution, for instance, norm. That is >> something like >> rnorm(HowMany,Min,Max,mean,sd) >> over rnorm(HowMany,mean,sd). >> I am wondering if >> >> qnorm(runif(HowMany, pnorm(Min,mean,sd), pnorm(Max,mean, sd)), >> mean, sd) >> >> is good. Any idea? Thanks. >> -james > > It would certainly work as intended! For instance: > > truncnorm<-function(HowMany,Min,Max,mean=0,sd=1){ > qnorm(runif(HowMany, pnorm(Min,mean,sd), pnorm(Max,mean, sd)), > mean, sd)} > Sample <- truncnorm(1000,-1,2.5) > hist(Sample,breaks=100) > > Hoping this helps, > Ted.
truncnorm<-function(HowMany,Min,Max,mean=0,sd=1){ qnorm(runif(HowMany, pnorm(Min,mean,sd), pnorm(Max,mean, sd)), mean, sd)} Sample <- truncnorm(100000,-1,2.5) hist(Sample,breaks=100) u0<-(-0.975)+0.05*(0:69) yy<-dnorm(u0) du<-min(diff(H$breaks)) Y <- 100000*yy*du/(pnorm(2.5)-pnorm(-1.0)) lines(u0,Y) Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.hard...@manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 11-Mar-09 Time: 00:05:23 ------------------------------ XFMail ------------------------------ ______________________________________________ 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.