You are on page 1of 28

Practical No: 1

Single channel queuing model.



Aim:-

Implement single channel queuing model using an excel
program. Perform the following tasks to implement the
following concept.
Write an excel program to do the following:
1. Use built in function to generate the n-digit random
numbers.
2. Distribution of time between arrivals.
a. Accept probability values for the interval-
arrival time between (1-6 minutes).
Generate the random digit assignment
b. Distribution of service time. Accept prob
values for the service time between
(1-6 minutes). Generate the random- digit
assignment.
3. Total No. of customers N=20.
4. Prepare a simulation table & answer the following
queries such as find
i) Avg. waiting time (in minutes)
ii) Probability that a customer has to
wait.
iii) Probability of idle server.
iv) Avg. Service time (in minutes)
v) Avg. time between arrivals (in minutes)
vi) Avg. waiting time of those who wait (in
minutes)
vii) Avg. time customer spends in the
system(in minutes)

Concept:-

Its calling population, the nature of the arrivals,
the service mechanism, the system capacity, & the queuing
discipline, describes a queuing system.
In the single channel queue, the calling population is
finite. Arrivals for service occur one at a time in a
random fashion; once they join the waiting line, they are
eventually served. In the addition, service times are of
some random length according to a probability
distribution, which does not change over time. The system
capacity has no limits. A single server or channel serves
units in order of their arrivals.
Arrivals & services are defined by the distribution of the
time between arrivals and the distribution of the service
times, respectively. For any single or multi channel queue,
the overall effective arrival rate must be less than the
total service rate, or the waiting line will grow without
bound, they are termed explosive or unstable.
Concepts in queuing system:
1. State: The no. of units in the system & the status of
the server, busy or idle.
2. Event: Set of circumstances that cause an instantaneous
change in the state of the system.
3. In a single channel queue there are only 2 possible
events that can change the state of the system. There
are entry of a unit into the system or the completion
of service.
4. The system includes a server, the unit being served &
units in the queue.
5. Simulation clock is used to track the simulated time.
Practical No: 2

Multi-channel queuing model.

Aim:-
Implement Multi-channel queuing model using a C program
with one server getting higher performance. Perform the
following tasks to implement the following concept.
Write a C program to do the following:
1) Use built-in function to generate the n-digited random
numbers
2) Distribution of inter-arrivals of cars.
a) Accept probability values for the inter-arrival time
between (1-4 minutes).
Generate the random-digit assignment.
b) Service distribution of Baker. Accept probability
values for the service time between (3-6 min). Generate
the random-digit assignment.
c) Service distribution of baker. Accept probability
values for the service time between (3-6 min). Generate
the random-digit assignment
3) Total no. of customers N=20.
4) Prepare a simulation table & answer the following
queries such as find.
1) Percentage of time Able & Baker individually were
busy over total service time.
2) Percentage of arrivals had to wait.
3) Avg. Service time (minutes)
4) Avg. time between arrivals (minutes)
5) Avg. waiting time for all customers
6) Avg. waiting time for all customers who had to
wait
Draw the conclusion regarding the system & cost of
waiting.
Concept:-
Its calling population, the nature of the arrival, the
service mechanism, the system capacity, and the queuing
discipline, describes a queuing system.
In the single channel queue, the calling population is finite.
Arrivals of service occur one at a time in a random fashion;
once they join the waiting line, they are eventually served.
In the addition, service times are of some random length
according to, a probability distribution that does not change
over time. The system capacity has no limit. Units are served
in order of their arrivals by a single channel or server.
Arrivals & services are defined by a distribution of the
time between arrivals & the distribution of the service times,
respectively. For any single or multi channel queue, the
overall effective arrival rate must be less than total service
rate, or the waiting line will grow without bound; they are
termed explosive or unstable.
Concepts in queuing systems:
1. State: The number of units in the system & the status of
the server busy or idle.
2. Event: Set of circumstances that cause an instantaneous
change in the state of the system.
3. In the single channel queue, there are only two possible
events that can change the state of the system. There are
the entries of the units into the system or the
completion of the service.
4. The system includes a server, the unit being served &
units in the queue.
5. Simulation clock is used to track the simulated time.
6. We find how well the current arrangement is working.


Practical No: 3

Inventory system

Aim:

Simulate Inventory system The Newspaper Sellers
Problem

Perform the following:
1. Use the in-built function to generate the n-digit
random number.
2. Random digit assignment for type of Newsday.
3. Accept the probabilities for the 3 types of news days:
Good, Fair, & Poor.
4. Prepare the simulation table for purchase of 70
newspapers & find out the total profit.
5. Total profit = (revenue from sales)-(cost of
newspaper)- (lost profit from excess demand) + (salvage
from sale of scrap papers)
6. For C/C++ programming use the library functions to
generate the n-digited random numbers.

Given:

Distribution of Newspapers Demanded

Demand Probability Distribution
Demand Good Fair Poor
40 0.03 0.10 0.44
50 0.05 0.18 0.22
60 0.15 0.40 0.16
70 0.20 0.20 0.12
80 0.35 0.08 0.06
90 0.15 0.04 0.00
100 0.07 0.00 0.00



Random digit assignment for Type of Newsday

Accept the probability for the three type of news days:
Good, Fair and Poor.

Prepare the simulation table for Purchase of 70
newspapers and find out the total profit.

Total Profit= (revenue from sales)(Cost of newspapers)
(lost profit from excess demand) + (Salvage from sale of
scrap papers).

Concept:-

An important class of simulation problems involves
inventory systems. A simple Inventory system includes a
periodic view length N, at which time the Inventory level is
checked. An order is make to bring the inventory up to the
level M. At the end of the first review period, an order
quantity, Q1, is placed. In the Inventory system the lead time
(i.e. the length of time between the placement & the receipt
of an order) is zero. Since the demands are not usually known
with certainty, the order quantities are probabilistic. Demand
is uniform over the time period. One possibility is that
demands all occur at the beginning of the cycle. Another is
that the lead-time is random of some positive length.

If the amount in Inventory drops below zero, indicating a
shortage. These units are backordered; when the order arrives,
the demand for the backordered items is satisfied first. To
avoid shortage, a buffer, or safety, stock would need to be
carried.

Carrying stock in inventory has an associated cost
attributed to the interest paid on the funds borrowed to buy
the items (this also could be considered as the loss from not
having the funds available for other investments purposes).
Other costs can be placed in the carrying or holding cost
column: renting of storage space, hiring guards, & so on. An
alternative to carrying high inventory is to make more
frequent reviews, & consequently, more frequent purchases or
replenishments. This has an associated cost: the ordering
cost. Also, there is a cost in being short. Customers may get
angry, at subsequent loss of good will. Larger inventories
decrease the possibilities of shortages. These costs must be
traded off in order to minimize the total cost of an inventory
system.

The total cost (pr total profit) of an inventory system
is the measure of performance. This can be affected by the
policy alternatives; the decision maker can control the
maximum inventory level M, & length of cycle N.


Practical No: 4
Discrete Distributions
Aim:

Implement discrete distribution for statistical models with
discrete random variable.
Perform the following task to show the concept.
a) Define discrete distribution.
b) Describe the purpose of discrete distribution.
c) Write C/C++/Excel code for
i) Bernoulli distribution.
1. Accept probability of success p, the probability of
failure, no. of trials from the user.
2. Calculate probability mass function (pmf) P(X),mean E(X)
& Variance V(X).
3. Display calculated terms.

ii) Binomial distribution.
1. Accept probability of success p, the probability of
failure, number of trails from the user.
2.Calculate probability mass function (pmf) P(X),mean
E(X) & Variance V(X)
3.Display calculated terms.

iii) Geometric Distribution:
1.Accept probability of success p, the probability of failure,
number of trials x to achieve first success from the user
2.Calculate probability mass function (pmf) P(X), mean E(X) &
Variance V(X)
3.Display calculated terms.

iv) Poisson Distribution:-
1.Accept the mean & variance from the user.
2.Calculate probability mass function (pmf)
3.Display calculated terms.

Concept:-

In modeling real world systems there are few situations where
the actions of the entities within the system under study cannot
be completely predicted in advance. There may be many causes of
variations. Some statistical models can describe these
variations.
Sampling the appropriate parameters can develop an appropriate
model. The model builder selects a known distribution form; make
an estimate of the parameters of the distribution.

Discrete random variable:
Let X is a random variable. If the number of possible values
is X is finite, or countable infinite, X is called a discrete
random variable. The possible values of X may be listed as x1,
x2 In the finite case the list terminates. In the countable
infinite case the list continues indefinitely.
Discrete random variables are used to describe random
phenomenon in which only integer values can occur.

1)Bernoulli trials & Bernoulli distribution.
Consider an experiment with n trials, each of which can
be a success or failure. These are called Bernoulli process if
the trials are independent; each trial has only two possible
outcomes (Success or Failure), & the probability of a success
remains constant from trial to trial.
Then p(x
1
, x
2
, x
3
x
n
)= p
1
(x
1
). p
2
(x
2
). p
3
(x
3
).. p
n
(x
n
).
& p
j
(x
j
)= p(x
j
) =p, x
j
=1=success, j=1,2,..,n.
=1-p=q x
j
=1=failure, j=1,2,.,n.
=0 otherwise.
The mean = E(Xj) = 0 . q+1 . p = p.
The variance = V(Xj) = [(0
2
.q)+ (1
2
.p)]- p
2
= p(1-p)

2) Binomial Distribution:-
The random variable X that denotes the number of
successes in n Bernoulli trials has a Binomial Distribution
given by p(x), where
p(x) =
n
C
x
p
x
q
n-x
, x=0,1,2,.....,n
= 0,
E(X) = np
V(X) = npq

3) Geometric distribution:-
The Geometric distribution is related to the sequence of
Bernoulli trials; the random variable of interest, X, is
defined to be the number of trials to achieve the first
success. The distribution of X is given by:
p(x) = q
x-1
p, x=1,2,.
= 0 , otherwise
E(X) = 1/p
V(X) = q/p
2
Geometric distribution is memoryless.

4) Poisson distribution:-
The Poisson distribution describes many random processes.
The Poisson pmf is given by:
p(x) = e
-@
@
x

x!
E(X) = @ =V(X)
F(x) = e
-@
@
i
/i!



Practical no 5

Continuous Distribution
Aim:-

Implement continuous distribution for statistical models
with discrete random variable.
Perform the following task to show the concept.
a) Define continuous distribution.
b) Describe the purpose of continuous distribution
c) Write Excel code for
i) Uniform distribution:
a) Accept parameters a,b, from the user
b)Calculate Probability distribution function (pdf) f(X),
Cumulative distribution function (cdf) F(X), where X is
distributed uniformly between a & b.
c)Display calculated terms.
ii) Exponential distribution:
a) Accept parameters lamda from the user
b)Calculate Probability distribution function (pdf) f(X),
Cumulative distribution function (cdf) F(X)
c)Display calculated terms.
iii) Gamma distribution:
a) Accept parameters | & C from the user
b) Calculate Probability distribution function (pdf) f(X),
Cumulative distribution function (cdf) F(X)
c) Display calculated terms.
iv) Erlang distribution:
a) Accept parameters k & C from the user
b) Calculate Probability distribution function (pdf) f(X),
Cumulative distribution function (cdf) F(X)
c) Display calculated terms.
v) Normal distribution:
a) Accept parameters mean & variance C
2
& transformation
variable
b) z=t-/o from the user.
c) Calculate Probability distribution function (pdf) f(X),
Cumulative distribution function (cdf) F(X)
d) Display calculated terms.
vi) Weibull Distribution:
a) Accept parameters of shape |>0, scale o>0 & scale v
from the user.
b) Calculate Probability distribution function (pdf)
f(X), Cumulative distribution function (cdf) F(X)
c) Display calculated terms.






vii) Triangular Distribution:
a)Accept parameters a, b, c from the user where a<b<c.
b)Calculate Probability distribution function (pdf) f(X),
Cumulative distribution function (cdf) F(X), where X is
distributed uniformly between a & b.
c)Display calculated terms.
viii) Lognormal distribution:
a) Accept parameters mean & variance C
2
from the user.
b) Calculate Probability distribution function(pdf)f(X),
Cumulative distribution function (cdf) F(X)
c) Display calculated terms.

Concept:-

In modeling real world systems there are few situations
where the actions of the entities within the system under
study cannot be completely predicted in advance. There
may be many causes of variations. Sampling the
appropriate parameters can develop an appropriate model.
The model builder selects a known distribution form; make
an estimate of the parameters of the distribution.

Continuous Random Variable:
If the range space Rx of the random variable is an
interval or a collection of intervals, X is called a
continuous variable. For a continuous variable X, the
probability that X lies between the interval [a,b] is
given by
P(a<X<b)= }
a
b
f(x)dx, where f(x) is called probability
density function.
It has following properties
a) f(x)>0 for all x in Rx.
b) (
Rx
f(x)dx=1
c) f(x)=0 if x is not in Rx.
The cdf is given by:
F(x)=}

x
f(t)dx
Some properties of cdf are
a) F is a non-decreasing function. If a<b, then F(a)< F(b)
b) Lim
x->
F(x)=1
c) Lim
x->
F(x)=0
Also p(a<x<b)=F(b)-F(a) for all a<b
1. Uniform Distribution.
A random variable X is uniformly distributed with
parameter on interval (a,b) if its pdf is given by

f(x)=1/b-a, a<X<b
= 0, otherwise.
The cdf is given by
F(x) =0 x<a
= x-a
b-a
P(x1<X<x2) = F(x2)-F(x2)
Uniform distribution plays a vital role in simulation.
Random numbers uniformly distributed between 0 & 1,
provide the means to generate random events.

2. Exponential distribution:
A random variable X is exponentially distributed with
parameter >0 if its pdf is given by,
f(x)=l* e
-lx
It is used to model interarrival times when arrivals are
completely random & to model service times that
are highly variable. In these instances, is the rate.
F(x) = 0, x<0
= 1-e
-it
, x>0
The Exponential distribution is memory less.

3. Gamma distribution:
A random variable X is Gamma distribution with
parameters of shape |, scale u if its pdf is given by
f(x) = |u (|ux)
|-1
e
-|ux
x>0
A random variable X is exponentially distributed with
parameter >0 if its pdf is given by, f(x)=1* e
-1*
It is used to model interarrival times when arrivals are
completely random & to model service time that are highly
variable. In these instances, is the rate.
F(x) = 0, x<0
= 1-e
-
, x>0

Practical No: 6

Generation of Random Numbers:
Aim: -
Implement Generation of Random Numbers using following
methods:
a. Define Linear Congruential Method.
b. Define Combined Linear Congruential Method
c. Write C/C++/Excel program for:
i) Linear Congruential Method.
a) Accept constant multiplier a, increment c,
modulus m, and seed X
0
from the user.


b) Display random numbers.
ii) Combined Linear Congruential Method

Concept:-

Linear Congruential Method: -
The Linear Congruential Method, initially proposed
by Lehmer(1951), produces a sequence of integer X
1
, X
2

between zero and m-1 according to the following recursive
relationship:
X
i+1
= (aX
i
+ c) mod m, i = 0, 1, 2, . ------(1)
The initial value X
0
is called the seed, a is called
the constant multiplier, c is the incremental and m is the
modulus. If c!=0 in equation (1), the form is called the
mixed Congruential Method. When c=0, the form is known as
the multiplicative Congruential Method. The selection of
the value for a, c, m and X
0
drastically affects the
statistical properties and cycle length. Variation of
equation (1), are quite common in the computer generation
of random numbers.

Combined Linear Congruential Method: -
As computing power has increased, the complexity of
the system that we are able to simulate gas also
increased. Random number generators will period 2
31
-1 =
2*10
9
.
One fruitful approach is to combine two or more
multiplicative congruential generators in such a way that
the combined generator has a good statistical property
and a longer period. The following result from
LEcuyer[1998] suggest how this can be done.

If W
i,1
, W
i,2
, ., W
i,k
, are any independent, discrete
valued random variables but one of them say W
i,1
, is
uniformly distributed on the integer 0 to m
i
-2, then



k
Wi = W
i,j
mod m
i
-1
j=1

is uniformly distributed on the integer 0 to m
i
-2.
To see how this result can be used to form combined
generator, let X
j,1,
X
j,2
, .., X
j, k
be the i
th
output
from k different multiplicative congruential
generator, where the jth generator has prime modulus
mj, and the multiplier aj is chosen so that the
period is mj-1. Then the jth generator is producing
integer Xi,j , that are approximately uniformly
distributed on 1 mj-1 and Wi,j-1 is approximately
uniformly distributed on 0 to mj-2 LEcuyer[1998]
thus suggested combined generator of the form:


k
Xi = (-1)
j-1
X
i,j
mod m
i
-1
j=1


with
Xi/m
1
Xi>0
Ri=
m
1
-1/m1 Xi=0

Notice that the (-1)
j-1
X
i,j
-1; for eg: if k=2, then
(-1
0
) (X
i,1
-1) (-1
1
) (X
i,2
-1) =
2
(-1
j-1
) (X
i,j
)

The maximum possible period for such a generator is
(m
1
-1)(m
2
-1).(m
k
-1)
P= 2
k-1


which is achieved by the following generator.
Practical No: 7

Test for Random Number

Aim: -

Implement a menu driven program to test the Random
Number using following test:

1) Frequency Test:
a) Chi-Square Test: Accept n random numbers, level of
significance number of intervals n, set of
observed values & set of estimated values
b) Kolmogrov Test: Accept n random numbers, number of
observations N, the set of Observations (Ri) where
1<=i<=N
2) Autocorrelation Test accept N random numbers, level of
level of significance the log m the starting number I,
M such that I +(m+1)m<=N.
3) Run Test: Accept n random numbers & level of significance
a) Run up and run down test: Accept the total number of
runs a find the mean and variance of a
b) Run below and above mean test: Accept the number of
individual observations above and below the mean as
N1 and N2 the total number of runs as b to find the
mean and variance for b.



Concept:-

1. The Frequency test is divided into 2 parts :
a. The Chi- Square test
b. Kolmogrov Smirov Test
a) The Chi- Square test: - The Chi- Square test uses the
sample statistic.
x
0
2
=n
n=1
(Oi-Ei)
2
/Ei
where Oi is the observed number in i
th
class, Ei is
the expected number in the i
th
class, and n is the number
of classes, for the uniform distribution, Ei, the
expected number in each class is given by:
Ei=N/n
For equally spaced classes, where N is the total number
of observations. It can be shown that the sampling
distribution of x
0
2
is approximately the Chi-Square
distribution with n-1 degrees of freedom.
b)Kolmogrov Test: - Kolmogrov Test is based on Largest
absolute deviation between F(x) and S
N
(x) over the range
of random variable. That is it is based on statistic
D=max |F(x)- S
N
(x)|
Sampling distribution D is known and is tabulated as a
function of N, for testing against a uniform cdf, the
test procedure follows these steps:

Step1.: Rank the data from smallest to largest. Let
R(i) denote the ith smallest observation, so that
R
(1)
<=R
(2)
<=<=R
(N)

Step2.: Compute D
+
=max1<=I<=N{i/N-R
(i)
}
D
-
=max1<=I<=N{R
(i)
-(I-1)/N}
Step3.: Compute D=max(D
+
, D
-
)
Step4.: Determine the critical value D

,from table
specified significance
Level and the given sample size N.
Inverse Transform Technique
Aim: -

Implement Inverse Transform technique to find the random
variants:
Perform the following task to show the concept.
a) Define Inverse Transform
b) Write Algorithm for continuous distributions.
Write C code for implementing Inverse Transform
technique.

i) Exponential distribution:
a. Accept parameters | & u from the user.
b. Calculate Probability distribution function
(pdf) f(X), Cumulative distribution function
(cdf) F(X).
c. Display calculated terms.
ii) Weibull distribution:
a. Accept parameters of shape |>0, scale o>0 &
scale v from the user.
b. Calculate Probability distribution function
(pdf) f(X), Cumulative distribution function
(cdf) F(X).
c. Display calculated terms.
iii) Gamma distribution:
a. Accept parameters lamda from the user.
b. Calculate Probability distribution function
(pdf) f(X), Cumulative distribution function
(cdf) F(X).
c. Display calculated terms.
iv) Triangular distribution:
a. Accept parameters a, b, & c from the user,
where asbsc.
b. Calculate Probability distribution function
(pdf) f(X), Cumulative distribution function
(cdf) F(X), where X is distributed uniformly
between a, b.
c. Display calculated terms

Concept:-

The inverse transform technique can be used to sample
from the exponential, the uniform, the Weibull, and the
Triangular distributions and the empirical distributions
Additionally, it is the underlying principle for sampling
from a wide variety of discrete distributions. The technique
will be explained in detail for the exponential distribution
and then applied to other distributions. It is most straight
forward, but not always more efficient, technique
computationally.

(1) Exponential distribution:
Exponential distribution, has probability density
function (pdf) given by


f(x)= e
- x
, x>= 0
0, x< 0


And cumulative distribution function (cdf) given by
F (x)= }
-
x
f (t)dt = 1- e
- x
, x>= 0
0, x< 0

The parameter can be interpreted as the mean number of
occurrences per time unit. For e.g. if inter arrival times
X
1
, X
2
, X
3
had an exponential distribution with rate , then
could be interpreted as the mean number of arrivals per
time unit, or the arrival rate. Notice that for any i
E (X
i
) = 1/
So that 1/ is the mean inter arrival time. The goal is
to develop a procedure for generating values X1, X2, X3 that
have an Exponential distribution. The inverse transform
technique can be utilized, at least in principle, for any
distribution, but it is most useful when the cdf, F(x), is of
such simple form that its inverse, F
-1
, can be easily
computed. A step-by-step procedure for the
Inverse transform technique, illustrated by the exponential
distributions, is as follows:
Step1: Compute the cdf of the desired random variable X. For
the exponential distribution, the cdf is F(x)= 1-e
- x

,x>=0.
Step2: Set F(x)=R on the range of X. For the exponential
distribution, it becomes 1-e
- x
= R on the range x>=0.
Since X is a random variable, it follows that 1-e
- x
is also
a random variable, here called R. As will be showed later, R
has a uniform distribution over the interval (0,1).
Step3: Solve the equation F (x)=R on the range of X. For the
exponential distribution, the solution proceeds as follows:
1-e
- x
= R
-e
- x
=1- R
- x= In (1-R)
X = -1/ In (1-R) (I)
Equation I is called a random variate generator for the
exponential distribution. In general, equation I is written
as X = F
-1
(R). Generating a sequence of values is accomplished
through step 4.
Step4: Generate (as needed) uniform random numbers R1, R2,
R3, and compute the desired random variates by:
Xi=F
-1
(R
i
)
For the exponential case, F
-1
(R) = (-1/)In (1-R) by
equation(I), so that
Xi = -1/1 In (1-R) (II)
For I = 1, 2, 3, one simplification that is usually employed
in equation(II), is to be replace 1-Ri by R
i
to yield
Xi = -1/1 In (1-R) (III)
which is justified since both R
i
and 1- R
i
are uniformly
distributed on (0,1)
(2)Weibull distribution: -
Step 1 : The cdf is given by
|
F (x)= 1-e
-(x/o)
x>=0
|
Step 2 : F (x)= 1-e
-(x/o)
= R
Step 3 : Solving for X in terms of yields
X=o[-In(1-R)]
1-|

(3)Gamma distribution
If X is following exponention with parameter
n
then

X is gamma(n, ). To generate one observation or
i=1
One random variable from gamma, we have to first
generate n observations from exponential say X
1
, X
2
,...,
X
n
then Y=

X is random observation from gamma.
n
Therefore Y=-1/*

log(1-R)
i=1
(4)Triangular distribution:
Consider a random variable X that has pdf.
This distribution is called Triangular distribution with
endpoints (0,2) & mode at 1. Its cdf is given by:
f(x) = 2(x-a) a s x s b
(b-a)(c-a)
= 2(c-x) b s x s c
(c-b)(c-a)
= 0 otherwise (I)

F(x) = (x-a)
2
a s x s b
(b-a)(c-a)
= (c-x)
2
b s x s c
(c-b)(c-a)
= 0 x>c (II)
By equation (I), 0<=X<=1 implies that 0<=R<=1/2, in which
case X=2R.
By equation (II), 1<=X<=2 implies that 1/2<=R<=1, in which
case X=2-2(1-R).
Thus X is generated by , F(x) = a+(b-a)R.

Source Codes:
(1)Exponential Distribution:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<math.h>
int n,i,m=100;
float x[50];
float a,X[100];
float R[100];
void main()
{
clrscr();
printf("\nRandom Variate Generation for Exponential
Distribution \n");
printf("\n-----------------------------------------\n");
printf("\nPdf of Exponential Distribution is lamda*e^(-
lamda*x)\n");
printf("\nCdf of Uniform Distribution is 1-e^(-
lamda*x)\n");
printf("\nEnter the value of lamda(mean): ");
scanf("%f",&a);
printf("\nEnter the number of random numbers you are
going to provide : ");
scanf("%d",&n);
randomize();
printf("\n\n Random Numbers Generated are:\n\n");
for (i=0;i<n;i++)
{
R[i]=random(100);
R[i]=R[i]/100;
printf("%.2f\t",R[i]);
}
printf("\n\nX[i]=(-1/lamda)*log(1-R[i])");
printf("\n\n----------------------------------------\n");
printf("| i | R[i] |X[i]=-1/a*log(1-
R[i]|\n");
printf("--------------------------------------------\n");
for(i=0;i<n;i++)
{
x[i]=(-1/a)*log(1-((double)R[i]));
printf("| %d\t | %lf\t | %f\t
|\n",i+1,R[i],x[i]);
}
printf("-------------------------------------\n");
getch();
}


Output


Random Variate Generation for Exponential Distribution

------------------------------------------------------

Pdf of Exponential Distribution is lamda*e^(-lamda*x)

Cdf of Uniform Distribution is 1-e^(-lamda*x)

Enter the value of lamda(mean): 2

Enter the number of random numbers you are going to provide :
5


Random Numbers Generated are:

0.98 0.89 0.06 0.91 0.77

-----------------------------------------------
| i | R[i] |X[i]=-1/a*log(1-R[i]|
-----------------------------------------------
| 1 | 0.980000 | 1.956012 |
| 2 | 0.890000 | 1.103637 |
| 3 | 0.060000 | 0.030938 |
| 4 | 0.910000 | 1.203973 |
| 5 | 0.770000 | 0.734838 |
----------------------------------------------


















(2) Weibull Distribution
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
int n,i;
double x[50];
float a,b,p;
float R[10],r1;
void main()
{
clrscr();
printf("\nRandom Variate Generation for Weibull Distribution
\n");
printf("\n--------------------------------------------------\n");
printf("\nPdf of Weibull Distribution is
(beta/(alpha)^beta)*(x^beta-1)(e^(-x/alpha)^beta)\n");
printf("\nCdf of Weibull Distribution is 1-e^(-x/alpha)^beta\n");
printf("\nEnter the value of alpha(float): ");
scanf("%f",&a);
printf("\nEnter the value of beta(float): ");
scanf("%f",&b);
p=a/b;
printf("\nEnter the number of random numbers you are going to
provide : ");
scanf("%d",&n);
randomize();
printf("\n\n Random Numbers Generated are:\n\n");
for (i=0;i<n;i++)
{
R[i]=random(100);
R[i]=R[i]/100;
printf("%.4f\t",R[i]);
}
printf("\n\n------------------------------------------\n");
printf("| i | R[i] | X[i]=(-
alpha/beta)*log(1-R[i])|\n");
printf("------------------------------------------\n");
for(i=0;i<n;i++)
{
r1=1-R[i];
x[i]=-p*log((double)r1);
printf("| %d\t | %f\t | %lf\t |\n",
i+1,R[i],x[i]);
}
printf("------------------------------------------\n");
getch();
}
Output

Random Variate Generation for Weibull Distribution

--------------------------------------------------

Pdf of Weibull Distribution is (beta/(alpha)^beta)*(x^beta-
1)(e^(-x/alpha)^beta)


Cdf of Weibull Distribution is 1-e^(-x/alpha)^beta

Enter the value of alpha(float): 2.5

Enter the value of beta(float): 3.4

Enter the number of random numbers you are going to provide : 10


Random Numbers Generated are:

0.0400 0.6300 0.4200 0.5700 0.2400 0.4200 0.1600 0.5900
0.0500 0.6100


----------------------------------------------------------
| i | R[i] | X[i]=(-alpha/beta)*log(1-R[i])|
----------------------------------------------------------
| 1 | 0.040000 | 0.030016 |
| 2 | 0.630000 | 0.731068 |
| 3 | 0.420000 | 0.400535 |
| 4 | 0.570000 | 0.620566 |
| 5 | 0.240000 | 0.201792 |
| 6 | 0.420000 | 0.400535 |
| 7 | 0.160000 | 0.128201 |
| 8 | 0.590000 | 0.655587 |
| 9 | 0.050000 | 0.037716 |
| 10 | 0.610000 | 0.692359 |
----------------------------------------------------------











(3) Gamma Distribution

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int n,i,j,n1;
float r[100],a,r1,r2,y,R[10];
clrscr();
printf("\nRandom variate generation using Gamma
Distribution\n");
printf("\n--------------------------------------------\n");
printf("\n\tConvolution Method");
printf("\n\t------------------");
printf("\nHow many gamma variates should be generated: ");
scanf("%d",&n1);
for(j=0;j<n1;j++)
{
printf("\nEnter the number of random numbers required to
generate %d gamma variate:",j+1);
scanf("%d",&n);
randomize();
printf("\n\n Random Numbers Generated are:\n\n");
for (i=0;i<n;i++)
{
R[i]=random(100);
R[i]=R[i]/100;
printf("%.4f\t",R[i]);
}
printf("\n\nEnter the value of lamda: ");
scanf("%f",&a);
r1=1.0;
r2=1.0;

for(i=0;i<n;i++)
{

r1= r2 *(1-R[i]);
r2=r1;
}
y=(-1/a) * log((double)r1);
printf("\n\nGamma variate is : %lf",y);
}
getch();
}

Output


Random variate generation using Gamma Distribution

--------------------------------------------------

Convolution Method
------------------
How many gamma variates should be generated: 2


Enter the number of random numbers required to generate 1
gamma variate:5


Random Numbers Generated are:

0.3800 0.9200 0.4500 0.4600 0.2600

Enter the value of lamda: 0.5


Gamma variate is : 9.037786

Enter the number of random numbers required to generate 2
gamma variate:4


Random Numbers Generated are:

0.5300 0.8300 0.0800 0.2300

Enter the value of lamda: 0.3


Gamma variate is : 9.572418













(4)Triangular Distribution

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<math.h>
int a,b,n,i;
float x[50],r1;
double R[100];
void main()
{
clrscr();
printf("\nRandom Variate Generation for Triangular
Distribution \n");
printf("\n---------------------------------------------------
--\n");
printf("\nEnter the number of random numbers you are going to
provide : ");
scanf("%d",&n);
randomize();
printf("\n\n Random Numbers Generated are:\n\n");
for (i=0;i<n;i++)
{
R[i]=random(100);
R[i]=R[i]/100;
printf("%.2f\t",R[i]);
}

printf("\nX[i]=(2*R[i])^1/2 0<=R[i]<=0.5");
printf("\n =2-((2*(1-R[i]))^1/2) 0.5<R[i]<=1");
printf("\n\n------------------------------------------\n");
printf("| i | R[i] | X[i] |\n");
printf("------------------------------------------\n");
for(i=0;i<n;i++)
{
r1=1-R[i];
if(R[i]>0 & R[i]<=0.5)
x[i]= sqrt(2*R[i]);
else if(R[i]>0.5 & R[i]<=1)
x[i]=2-(sqrt(2*r1));
printf("| %d\t | %lf\t | %f\t |\n",i+1,R[i],x[i]);
}
printf("------------------------------------------\n");

getch();
}

Output

Random Variate Generation for Triangular Distribution

Enter the value of lamda(mean): 1

Enter the number of random numbers you are going to provide :
20


Random Numbers Generated are:

0.92 0.71 0.62 0.09 0.32 0.76 0.72 0.77
0.63 0.07
0.70 0.61 0.98 0.12 0.64 0.35 0.63 0.36
0.33 0.02


-----------------------------------------------
| i | R[i] | X[i]=-1/alog(1-R[i]|
-----------------------------------------------
| 0 | 0.920000 | 2.525729 |
| 1 | 0.710000 | 1.237874 |
| 2 | 0.620000 | 0.967584 |
| 3 | 0.090000 | 0.094311 |
| 4 | 0.320000 | 0.385662 |
| 5 | 0.760000 | 1.427116 |
| 6 | 0.720000 | 1.272966 |
| 7 | 0.770000 | 1.469676 |
| 8 | 0.630000 | 0.994252 |
| 9 | 0.070000 | 0.072571 |
| 10 | 0.700000 | 1.203973 |
| 11 | 0.610000 | 0.941609 |
| 12 | 0.980000 | 3.912023 |
| 13 | 0.120000 | 0.127833 |
| 14 | 0.640000 | 1.021651 |
| 15 | 0.350000 | 0.430783 |
| 16 | 0.630000 | 0.994252 |
| 17 | 0.360000 | 0.446287 |
| 18 | 0.330000 | 0.400478 |
| 19 | 0.020000 | 0.020203 |
---------------------------------------------






Practical No:09

Acceptance-Rejection Technique:

Aim: -

Implement methods for generation of random variates, X for a
given Distribution.
Perform the following tasks to show the concept:
a. Define Acceptance Rejection Technique.
b. Write algorithm for Acceptance Rejection Technique.
c. Write C/C++/excel code for implementing:
1) Poisson distribution
a) Accept value of mean o, arrivals n and
probability of random variate p.
b) Display the calculated Poisson variates.

Concept:-

Method for generating random variates, X, uniformly
distributed between and one way to proceed would be to
follow these steps:
Step1 : Generate a random numbers R.
Step2 : a) If R>=1/4, accept X=R then goto step 3.
b) If R<=1/4, reject R, and then return to step 1.
Step3 : If another uniform random variate on [1/4, 1] is
needed repeat the procedure
beginning at step1, if not stop.

Poisson Distribution : -
A Poisson random variable, N with mean o>0 has pmf
p(n)= P(N=n)=e
-o
o
n
/n!, n=0,1,2,3,.
N can be interpreted as the number of arrivals from
Poisson arrival process in one
unit of time.
Step1 : Set n=0, P=1
Step2 :.Generate the random numbers R
n+1
and replace P by P*R
n+1
.
Step3 : If P<e-o, then accept N=n. Otherwise, reject the
current n, increase n by one, &
return to step2.

To generate one poisson variate, if N=n, then n+1 random
numbers are required, so the average number is given by:
E(N+1)= o+1, which is quite large if the mean, o, of the
poisson distribution is large.

`

You might also like