R Programming

How to generate random numbers from various statistical distributions using R

We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

In this post, we show how to generate random numbers into vector and matrix in R programming, from various statistical distributions. Specifically, we focus several basic and widely used statistical distributions here, namely, normal distribution, continuous uniform distribution, binomial distribution and Poisson distribution.

R language use alphabet r plus an abbreviation string for statistical distribution for generate random numbers. For example, rnorm(), runif(), rbinom() and rpois() are the functions for creating random numbers from normal, uniform, binomial, and Poisson distributions, respectively.

  1. rnorm()

Normal distribution models continuous random variables having a bell-shape in its probability density curve. It is symmetrical along the central or mean point of the available values. The basic form of rnorm() is

rnorm(N, mean, sd)

Where N is for how many random variates to be generated;

and mean and sd represent the mean and standard deviation of the normal distribution. The default values are mean = 0, and sd =1.

Following code shows first 10 random numbers drawn from standard normal distribution, then 10 random variates drawn from a normal distribution with mean 32 and standard deviation 2. Then we draw 50 random numbers from a normal distribution and arrange them into a matrix with 10 rows and 5 columns.

#10 random numbers drawn from standard normal distribution
#mean = 0, sd = 1
vec_norm1 <- rnorm(10)   

vec_norm1
#result
[1] -1.8879117 -1.1810440  0.7014863  0.1456266  0.4672270  0.1537036
 [7] -1.4581615 -0.3243113 -0.5013742  0.2260546

#10 random numbers drawn from normal distribution with mean 
# 32 sd 2
vec_norm2 <- rnorm(10, mean=32, sd=2)

vec_norm2
#result
[1] 30.13605 33.13508 32.13494 34.71462 33.15386 33.17344 35.02709
 [8] 31.40494 30.63439 33.03915

# a matrix of 50 normal variates from normal distribution #with mean 32 sd 2
mat_norm <- matrix(rnorm(50,mean=32, sd=2),nrow=10)
mat_norm 
#result
         [,1]     [,2]     [,3]     [,4]     [,5]
 [1,] 29.93599 30.32043 30.15758 29.31383 29.77837
 [2,] 31.75832 32.18018 31.84313 33.10662 32.68404
 [3,] 32.10932 31.56479 31.48874 31.58652 28.99705
 [4,] 31.16816 30.83927 31.81911 31.00349 32.85473
 [5,] 33.65444 31.87613 30.46526 29.91282 29.66126
 [6,] 30.51390 34.09477 33.76685 30.37538 32.11878
 [7,] 33.11698 29.04160 32.76324 27.96582 32.75961
 [8,] 36.61849 33.93343 34.12846 28.67765 32.44512
 [9,] 33.05164 34.46608 33.18443 34.78499 32.15884
[10,] 31.77686 29.89119 32.49529 32.20178 35.01148

2. runif()

A continuous uniform distribution has constant probability density in a range of values. runif(N, min, max) will generate random numbers from a continuous distribution with lower bound and upper bound set at values of min and max. Default values for these two parameters are 0 and 1. The following example first draws 25 uniform variates with lower and upper bound set to 0 and 1, then 10 random numbers drawn from a uniform distribution with 3 and 8 for lower and upper bound.

#Generating 25 numbers from uniform distribution with lower 
#bound 0 and upper bound 1.
vec_uni_1 <- runif(25)  
vec_uni_1

#result
[1] 0.78657848 0.04305426 0.63714236 0.20140764 0.34610064 0.23692078
 [7] 0.97360202 0.10545540 0.20266333 0.22997894 0.68824237 0.32685904
[13] 0.78385576 0.47163550 0.13340535 0.05540619 0.37276432 0.91397896
[19] 0.03221100 0.20967881 0.02774103 0.27431753 0.09920050 0.78022747
[25] 0.07692057

#10 uniform variates between 3 and 8
vec_uni_2 <- runif(10, min=3, max=8) 
vec_uni_2

#result
[1] 7.756845 5.988585 6.537091 4.454690 7.967026 6.935875 7.769008 6.646147 3.704943 3.038014

3. rbinom()

Binomial distribution is used to model the probability distribution for discrete success numbers from N Bernoulli trials, with a constant success rate p for each trial. rbinom(N, size=n, prob = p) in R will draw N binomial random numbers from n Bernoulli trial with success rate p. Following codes show example of drawing 8 random numbers from binomial distribution where total trial is 10 and success rate is 0.2. Then we draw such 50 random numbers and arrange them into a matrix with 10 rows and 5 columns.

#generate 8 random variables from binomial distribution where
# total trial is 10, p = 0.2
rbinom(8, size=10, prob = 0.2)

#output
[1] 2 3 4 0 2 4 1 1

#generate a matrix (10 rows) of 50 random variables of 
#binomial distribution where size = 10, p = 0.2
matrix(rbinom(50, size = 10, prob = 0.2),nrow=10)

#result
     [,1] [,2] [,3] [,4] [,5]
 [1,]    0    4    4    1    2
 [2,]    2    0    3    1    0
 [3,]    4    2    1    3    1
 [4,]    2    1    2    3    2
 [5,]    1    2    4    3    0
 [6,]    1    2    2    0    2
 [7,]    1    1    2    2    1
 [8,]    4    1    2    2    5
 [9,]    1    1    3    3    1
[10,]    2    2    3    1    0

4. rpois()

Poisson distribution models the probability distribution of discrete number of occurring events during a determined time interval, with a mean occurring number of events. rpois(N, miu) will generate N random values from a Poisson distribution in which the mean occurring events during this time interval is miu. In the following example, we first draw 20 Poisson random variates with mean occurring events is 10, then draw 50 such random values and arrange them into a matrix with 10 rows and 5 columns.

# to create 20 random number of Poisson distribution
# with mean occurring numbers during this interval is 10

rpois(20, 10)

#output
[1] 11  9 10 12  8  9  9  7  9 10  7  8  7  8  9 10 13 10  9  9

#a matrix of 50 poisson variates with mean 10
matrix(rpois(50, 10),nrow=10)

     [,1] [,2] [,3] [,4] [,5]
 [1,]   10   16   13    4    9
 [2,]   10    8   13    8    7
 [3,]   17   14    9    8   16
 [4,]   15    3   10   10    6
 [5,]   16    8    9   13   10
 [6,]   13   11   11    7   15
 [7,]   11    9    8    7   11
 [8,]   11   12   11    7   10
 [9,]   14    9    7    4   12
[10,]    6    6    7   13    5

You can also watch R tutorial full video from our YouTube channel.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago