Random numbers are widely used in statistics. For example, in generation of random samples and sampling from finite population. It is a computational device designed to generate a sequence of random numbers or symbols that lack any pattern. The many application of randomness have lead to the development of several different methods for generating random data. We have provided the syntax to generate random numbers from various discrete distributions using standard uniform distibution. Also, examples are provided to illustrate the syntax.
[1] 0.02774016 0.35148261 0.64503169 0.08251357 0.28122216 0.90209830
[7] 0.41191685 0.85152406 0.18127632 0.23471648
if \(U\) \(\sim\) \(U(0,1)\) and \(p\) is specified \[\begin{equation*} \text{Define}\ \ X = \begin{cases} 1 & \text{if }\ U \leq p \\ 0 & \text{otherwise} \end{cases} \end{equation*}\]Then \(X\) \(\sim\) \(B(1, p)\) distribution .
# Generate one random number from B(1, p), p=0.5 using the above syntax
p=0.5
U=runif(1)
if(U<=p) X=1 else X=0
print(X)[1] 1
# Generate ten random numbers from B(1, p), p=0.5 using the above syntax
n=10
X={}
p=0.5
for(i in 1:n)
{
U=runif(1)
if(U<=p) {X[i]=1} else {X[i]=0}
}
print(X) [1] 1 0 1 1 0 1 1 0 1 0
if \(U_i\) \(\sim\) \(U(0,1)\), \(\forall\) \(i\) \(=\) 1, 2, …, \(n\) and \(n\) and \(p\) are specified \[\begin{equation*} \text{Define}\ \ X_i = \begin{cases} 1 & \text{if }\ U_i \leq p \\ 0 & \text{otherwise} \end{cases};\ X_i\ \text{are independent} \end{equation*}\]Then \(Y\) \(=\) \(\sum_{i=1}^{n} X_i\) \(\sim\) \(B(n, p)\) distribution .
# Generate one random number from B(10, 0.5).
n=10
X={}
p=0.5
for(i in 1:n)
{
U=runif(1)
if(U<=p) X[i]=1 else X[i]=0
}
Y=sum(X)
print(Y) [1] 4
# Generate five random number from B(10, 0.5)
m=5 #No. of Binomial random variales
n=10 #No. of Bernoulli random variales
Y={}
p=0.5
for(j in 1:m)
{
X={}
for(i in 1:n)
{
U=runif(1)
if(U<=p) X[i]=1 else X[i]=0
}
Y[j]=sum(X)
}
print(Y)[1] 5 4 5 7 8
Let \(U_i\) \(\sim\) \(U(0,1)\), \(\forall\) \(i\) \(=\) 1, 2, …, \(n\) and \(U_i\) are independent and identically distributed and \(\lambda\) specified \[\begin{equation*} \text{Define}\ \ X = \displaystyle \min_{n \geq 0}\left[\prod_{i = 1}^{n+1}U_i\leq e^{-\lambda}\right] \end{equation*}\]Then \(X\) \(\sim\) \(P(\lambda)\) distribution .
# Generate ten random numbers from P(lambda)
n=10
lm=2
x={}
for(i in 1:n)
{
P=1;x[i]=0
while(P>exp(-lm))
{
P=P*runif(1)
x[i]=x[i]+1
}
x[i]=x[i]-1
}
print(x) [1] 3 0 3 2 0 0 2 2 4 0
if \(U\) \(\sim\) \(U(0,1)\) then \[\begin{equation*} X = \text{integer}\left[\frac{log(U)}{log(1-p)}\right] \end{equation*}\]\(X\) \(\sim\) \(G(p)\) distribution .
# Generate one random number from G(0.5) distribution
p=0.5
U=runif(1)
X=as.integer(log(U)/log(1-p))
print(X)[1] 0
#Generate ten random numbers from G(0.5) distribution
n=10
X={}
p=0.5
for(i in 1:n)
{
U=runif(1)
X[i]=as.integer(log(U)/log(1-p))
}
print(X) [1] 0 1 1 0 6 0 1 0 0 2
Let \(U_i\) \(\sim\) \(U(0,1)\), \(\forall\) \(i\) \(=\) 1, 2, …, \(n\) and \(U_i\) are independent and identically distributed and \(k\), \(p\) are specified \[\begin{equation*} \text{Define}\ \ X_i = \text{integer}\left[\frac{log(U)}{log(1-p)}\right]; \ X_i\ \text{are independent} \end{equation*}\]Then \(Y\) \(=\) \(\sum_{i=1}^{k} X_i\) \(\sim\) \(NB(k, p)\) distribution .
# Generate one random number from NB(10,0.5) distribution
r=10
p=0.5
X={}
for(i in 1:r)
{
U=runif(1)
X[i]=as.integer(log(U)/log(1-p))
}
Y=sum(X)
print(Y)[1] 12
# Generate five random numbers from NB(10, 0.5) distribution
m=5 # NB random numbers
r=10
p=0.5
Y={}
for(j in 1:m)
{
X={}
for(i in 1:r)
{
U=runif(1)
X[i]=as.integer(log(U)/log(1-p))
}
Y[j]=sum(X)
}
print(Y)[1] 16 11 6 7 11
Let \(U_i\) \(\sim\) \(U(0,1)\), \(\forall\) \(i\) \(=\) 1, 2, …, \(n\) and \(U_i\) are independent and identically distributed and \(M\), \(N\), and \(n\) are specified \[\begin{equation*} \text{Define}\ \ X_i = \begin{cases} 1 & \text{if }\ U_i \leq \frac{N-\sum X}{N-(\sum X-1)} \\ 0 & \text{otherwise} \end{cases} \end{equation*}\]Then \(X\) \(\sim\) \(HG(N, M, n)\) distribution .
# Generate three random numbers from H(10, 5, 3) distribution
N=10
M=5
n=3
x={}
for(j in 1:n)
{
x[j]=0
for(i in 1:n)
{
if(runif(1)<=((M-x[j])/(N-i+1))) x[j]=x[j]+1
}
}
print(x)[1] 1 2 2
Mr. Dadasaheb G. Godase is a PhD scholar in the Department of Statistics at Shivaji University, Kolhapur, India. Presently, he is working as an INSPIRE fellow of Department of Science and Technology, New Delhi, India in the Department of Statistics, Shivaji University, Kolhapur.
Mr. Shubham R. Shinde is a PG student in the Department of Statistics at Shivaji University, Kolhapur, India.