I want to generate 50 numbers with rnorm
, with two criteria.
If there are numbers <99
or >101
(i.e., outside 99-101
), omit them and run rnorm
again until 50 total numbers that meet criteria are generated.
- This is unrelated to statistics. I'm simply trying to learn how to use while loops.
I suspect the
rnorm(50,
part on line 3., is computationally inefficient -- any advice there would be great.The main problem I have is that although this code works, it goes on for ever. It needs to terminate when there are 50 observations that meet criteria. Thus far, I have tried unsuccessfully to use
if
andbreak
to do this...
Code so far:
1. z = rnorm(50, mean = 100, sd = 10)
2. while( match(TRUE, z > 99) > 0 | match(TRUE, z < 101) > 0 ) {
3. z = c( z[z >= 99 & z <= 101], rnorm(50, mean = 100, sd = 10 ))
4. }