R Language Generating random numbers using various density functions


Example

Below are examples of generating 5 random numbers using various probability distributions.

Uniform distribution between 0 and 10

runif(5, min=0, max=10)
[1] 2.1724399 8.9209930 6.1969249 9.3303321 2.4054102

Normal distribution with 0 mean and standard deviation of 1

rnorm(5, mean=0, sd=1)
[1] -0.97414402 -0.85722281 -0.08555494 -0.37444299  1.20032409

Binomial distribution with 10 trials and success probability of 0.5

rbinom(5, size=10, prob=0.5)
[1] 4 3 5 2 3

Geometric distribution with 0.2 success probability

rgeom(5, prob=0.2)
[1] 14  8 11  1  3

Hypergeometric distribution with 3 white balls, 10 black balls and 5 draws

rhyper(5, m=3, n=10, k=5)
[1] 2 0 1 1 1

Negative Binomial distribution with 10 trials and success probability of 0.8

rnbinom(5, size=10, prob=0.8)
[1] 3 1 3 4 2

Poisson distribution with mean and variance (lambda) of 2

rpois(5, lambda=2)
[1] 2 1 2 3 4

Exponential distribution with the rate of 1.5

rexp(5, rate=1.5)
[1] 1.8993303 0.4799358 0.5578280 1.5630711 0.6228000

Logistic distribution with 0 location and scale of 1

rlogis(5, location=0, scale=1)
[1]  0.9498992 -1.0287433 -0.4192311  0.7028510 -1.2095458

Chi-squared distribution with 15 degrees of freedom

rchisq(5, df=15)
[1] 14.89209 19.36947 10.27745 19.48376 23.32898

Beta distribution with shape parameters a=1 and b=0.5

rbeta(5, shape1=1, shape2=0.5)
[1] 0.1670306 0.5321586 0.9869520 0.9548993 0.9999737

Gamma distribution with shape parameter of 3 and scale=0.5

rgamma(5, shape=3, scale=0.5)
[1] 2.2445984 0.7934152 3.2366673 2.2897537 0.8573059

Cauchy distribution with 0 location and scale of 1

rcauchy(5, location=0, scale=1)
[1] -0.01285116 -0.38918446  8.71016696 10.60293284 -0.68017185

Log-normal distribution with 0 mean and standard deviation of 1 (on log scale)

rlnorm(5, meanlog=0, sdlog=1)
[1] 0.8725009 2.9433779 0.3329107 2.5976206 2.8171894

Weibull distribution with shape parameter of 0.5 and scale of 1

rweibull(5, shape=0.5, scale=1)
[1] 0.337599112 1.307774557 7.233985075 5.840429942 0.005751181

Wilcoxon distribution with 10 observations in the first sample and 20 in second.

rwilcox(5, 10, 20)
[1] 111  88  93 100 124

Multinomial distribution with 5 object and 3 boxes using the specified probabilities

rmultinom(5, size=5, prob=c(0.1,0.1,0.8))
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    1    1    0
[2,]    2    0    1    1    0
[3,]    3    5    3    3    5