Reputation: 15
I am trying to quickly round numbers from a normal distribution to a whole number between 1 and 10. I want to pull a random number from a distribution, but then round to a whole number between 1 and 10. If I run: rnorm(1, 10, 2.5) I get something like 12.5689 which is greater than 10. I know I can use round or ceiling to round these values, but I am unsure how get it down to 10 without creating a function that has to sift thru each data point for each iteration and correct them. I have the same problem on the lower bound with 1 and finding values that are either negative or less than 1. thanks!
Upvotes: 0
Views: 713
Reputation: 145775
pmin
and pmax
are your friends:
pmin(10, pmax(1, round(rnorm(10, 10, 2.5))))
# [1] 9 8 9 8 10 10 10 9 10 10
Upvotes: 2