I am using rnorm but the outputs I receive are sometimes negative. How do I create a restriction so that the outputs cannot be below 0? Example:
output = rnorm(1, 800/20, sqrt(800))
I am using rnorm but the outputs I receive are sometimes negative. How do I create a restriction so that the outputs cannot be below 0? Example:
output = rnorm(1, 800/20, sqrt(800))
Why not abs(rnorm(1, 800/20, sqrt(800))
? rnorm
was written to give numbers from a normal distribution. Perhaps you are looking to get output from a truncated distribution. In that case, you might want to have a look at the truncnorm
package.
library(truncnorm)
rtruncnorm( 1, a=0, b=Inf, 800/20, sqrt(800))
x = seq(-20,200,by=0.01)
y = dtruncnorm(x, a=0, b=Inf, 800/20, sqrt(800) )
plot(x,y,type="l",main="Density of a truncated normal distribution")
The Poisson distribution takes only positive values. Otherwise golbasche solution seems perfect.
hist(rpois(100, 5))