TRIBHUVAN UNIVERSITY
PATAN MULTIPLE CAMPUS
PATANDHOKA, LALITPUR NEPAL
“LAB REPORT”
SUBJECT : SIMULATION AND MODELLING
Submitted By: Submitted To:
Name: AASHISH PANTA Department of CSIT
College Roll No.: 25/079
Symbol number : 79010004
1. Generate the random numbers using MS-Excel function.
To generate random numbers in MS-EXCEL we use the RAND function.
Syntax:
=RAND()
Steps to use ‘RAND’ function in MS-Excel:
1. OpenMS-Excel.
2. Selectacell.
3. Typethefollowingformulaintothecell:
=RAND()
4. PressENTER.
5. TheRandomNumberisGeneratedbetween0to1.
Example of generating 10 random numbers using MS-Excel:
2. Write a C program to find the growth in national consumption for five
years using Distributed Lag Model given below:
I=2+0.1Y1
Y = 45.45 + 2.27 (I +G)
T=0.2Y
C = 20 + 0.7 (YT)
Assume the initial value of Y1 is 80 and take the governmental expenditure
in the 5 years to be as follows:
Year G
1 20
2 25
3 30
4 35
5 40
Code:
#include <stdio.h>
int main()
{
// Given initial values and constants
doubleY1=80; //InitialvalueofY1
double G[] = {20, 25, 30, 35, 40}; // Government expenditure for 5 years
doubleI,Y,T,C; //Variablestostoreintermediateandfinalresults
// Arrays to store yearly results for display
double I_vals[5], Y_vals[5], T_vals[5], C_vals[5];
// Loop through each year and calculate the values
for (int year = 0; year < 5; year++)
{
// Calculate Investment (I)
I=2+0.1*Y1;
// Calculate National Income (Y)
Y = 45.45 + 2.27 * (I + G[year]);
// Calculate Taxes (T)
T=0.2*Y;
// Calculate Consumption (C)
C=20+0.7*(Y-T);
// Store the results
I_vals[year] = I;
Y_vals[year] = Y;
T_vals[year] = T;
C_vals[year] = C;
// Update Y1 for the next year
Y1 = Y;
}
// Print the results for each year
printf("Year\tG\tI\tY\tT\tC\n");
for (int year = 0; year < 5; year++)
{
printf("%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", year + 1, G[year], I_vals[year], Y_vals[year],
T_vals[year], C_vals[year]);
}
return 0;
}
Output:
3. Write a C program to generate the random numbers using linear
congruential
Code:
#include <stdio.h>
// Define the parameters for the LCG
#define A 1664525
#define C 1013904223
#define M 4294967296 // 2^32
#defineSEED42 //Initialseedvalue
// Function to generate the next random number
unsigned long lcg(unsigned long *state) {
*state = (A * (*state) + C) % M;
return *state;
}
int main() {
unsigned long state = SEED; // Initialize the state with the seed
int num_random_numbers = 10; // Number of random numbers to generate
// Print the generated random numbers
printf("Random numbers generated using LCG:\n");
for (int i = 0; i < num_random_numbers; i++) {
printf("%lu\n", lcg(&state));
}
return 0;
}
Output:
4. Write a C program to implement the Kolmogorov-Smirnov Test
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Function prototypes
void sort(double arr[], int n);
double max(double a, double b);
double ks_test(double data1[], int n1, double data2[], int n2);
int main() {
int n1, n2;
printf("Enter the size of the first data set: ");
scanf("%d", &n1);
double *data1 = (double *)malloc(n1 * sizeof(double));
if (data1 == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Enter the elements of the first data set:\n");
for (int i = 0; i < n1; i++) {
scanf("%lf", &data1[i]);
}
printf("Enter the size of the second data set: ");
scanf("%d", &n2);
double *data2 = (double *)malloc(n2 * sizeof(double));
if (data2 == NULL) {
printf("Memory allocation failed\n");
free(data1);
return 1;
}
printf("Enter the elements of the second data set:\n");
for (int i = 0; i < n2; i++) {
scanf("%lf", &data2[i]);
}
double ks_statistic = ks_test(data1, n1, data2, n2);
printf("KS Statistic: %f\n", ks_statistic);
free(data1);
free(data2);
return 0;
}
// Function to sort an array
void sort(double arr[], int n) {
int i, j;
double temp;
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
// Function to return the maximum of two values
double max(double a, double b) {
return (a > b) ? a : b;
}
// Function to calculate the KS statistic
double ks_test(double data1[], int n1, double data2[], int n2) {
sort(data1, n1);
sort(data2, n2);
inti=0,j=0;
double d = 0.0;
double cdf1 = 0.0, cdf2 = 0.0;
while (i < n1 && j < n2) {
if (data1[i] < data2[j]) {
cdf1 = (double)(i + 1) / n1;
i++;
} else if (data1[i] > data2[j]) {
cdf2 = (double)(j + 1) / n2;
j++;
} else {
cdf1 = (double)(i + 1) / n1;
cdf2 = (double)(j + 1) / n2;
i++;
j++;
}
d = max(d, fabs(cdf1 - cdf2));
}
// Account for remaining elements in data1
while (i < n1) {
cdf1 = (double)(i + 1) / n1;
d = max(d, fabs(cdf1 - cdf2));
i++;
}
// Account for remaining elements in data2
while (j < n2) {
cdf2 = (double)(j + 1) / n2;
d = max(d, fabs(cdf1 - cdf2));
j++;
}
return d;
}
Output:
5. Write a C program to implement the Chi-Square Test
Code:
#include <stdio.h>
#include <math.h>
// Function to calculate Chi-Square Test statistic
double chiSquareTest(int observed[], int expected[], int size) {
double chiSquare = 0.0;
for (int i = 0; i < size; i++) {
if (expected[i] != 0) { // Avoid division by zero
double difference = observed[i] - expected[i];
chiSquare += (difference * difference) / expected[i];
}
}
return chiSquare;
}
int main() {
int size;
// Input the number of categories
printf("Enter the number of categories: ");
scanf("%d", &size);
int observed[size];
int expected[size];
// Input observed frequencies
printf("Enter the observed frequencies:\n");
for (int i = 0; i < size; i++) {
printf("Observed frequency for category %d: ", i + 1);
scanf("%d", &observed[i]);
}
// Input expected frequencies
printf("Enter the expected frequencies:\n");
for (int i = 0; i < size; i++) {
printf("Expected frequency for category %d: ", i + 1);
scanf("%d", &expected[i]);
}
// Calculate Chi-Square statistic
double chiSquare = chiSquareTest(observed, expected, size);
printf("Chi-Square Statistic: %.2f\n", chiSquare);
return 0;
}
Output:
6. Write a C program to implement Gap Test
Code:
#include <stdio.h>
#define MAX_SIZE 1000
// Function to count gaps between occurrences of a specific value
void gapTest(int sequence[], int size, int value) {
int gaps[MAX_SIZE] = {0}; // Array to store the gaps
int gapCount = 0;
int gapIndex = 0;
// Find gaps between occurrences of the specified value
for (int i = 0; i < size; i++) {
if (sequence[i] == value) {
if (gapCount > 0) {
gaps[gapIndex++] = gapCount; // Store the gap
}
gapCount = 0; // Reset gap counter
} else {
gapCount++; // Increment gap counter
}
}
// Print the gaps
printf("Gaps between occurrences of %d:\n", value);
for (int i = 0; i < gapIndex; i++) {
printf("%d ", gaps[i]);
}
printf("\n");
}
int main() {
int sequence[MAX_SIZE];
int size;
int value;
// Input the size of the sequence
printf("Enter the number of elements in the sequence: ");
scanf("%d", &size);
// Input the sequence of numbers
printf("Enter the sequence of numbers:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &sequence[i]);
}
// Input the value to test gaps for
printf("Enter the value to test gaps for: ");
scanf("%d", &value);
// Perform the Gap Test
gapTest(sequence, size, value);
return 0;
}
Output:
7. Write a C program to implement Auto Correlation Test
Code:
#include <stdio.h>
// Function to calculate the mean of an array
double mean(int data[], int size) {
double sum = 0.0;
for (int i = 0; i < size; i++) {
sum += data[i];
}
return sum / size;
}
// Function to calculate the variance of an array
double variance(int data[], int size, double mean) {
double sum = 0.0;
for (int i = 0; i < size; i++) {
sum += (data[i] - mean) * (data[i] - mean);
}
return sum / size;
}
// Function to calculate autocorrelation at a given lag
double autocorrelation(int data[], int size, int lag) {
double mean_val = mean(data, size);
double var = variance(data, size, mean_val);
if (lag >= size) {
return 0.0; // Lag is too large
}
double cov = 0.0;
for (int i = 0; i < size - lag; i++) {
cov += (data[i] - mean_val) * (data[i + lag] - mean_val);
}
cov /= size;
return cov / var;
}
int main() {
int size;
int max_lag;
// Input the size of the sequence
printf("Enter the number of elements in the sequence: ");
scanf("%d", &size);
int data[size];
// Input the sequence of numbers
printf("Enter the sequence of numbers:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &data[i]);
}
// Input the maximum lag to test
printf("Enter the maximum lag to test: ");
scanf("%d", &max_lag);
// Perform the Autocorrelation Test
printf("Autocorrelation values for lags from 0 to %d:\n", max_lag);
for (int lag = 0; lag <= max_lag; lag++) {
double result = autocorrelation(data, size, lag);
printf("Lag %d: %.4f\n", lag, result);
}
return 0;
}
Output:
8. Write a C program to implement Poker Test (For Three digit and Four
Digit)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1000
#define HAND_SIZE_3 1000
#define HAND_SIZE_4 10000
// Function to count frequencies of hands
void countFrequencies(int data[], int size, int handSize, int frequencies[]) {
int numHands = (handSize == 3) ? HAND_SIZE_3 : HAND_SIZE_4;
// Initialize frequencies array
for (int i = 0; i < numHands; i++) {
frequencies[i] = 0;
}
// Count occurrences of each hand
for (int i = 0; i < size - handSize + 1; i++) {
int hand = 0;
for (int j = 0; j < handSize; j++) {
hand = hand * 10 + data[i + j];
}
frequencies[hand]++;
}
}
// Function to perform the Poker Test
void pokerTest(int data[], int size, int handSize) {
int numHands = (handSize == 3) ? HAND_SIZE_3 : HAND_SIZE_4;
int frequencies[numHands];
countFrequencies(data, size, handSize, frequencies);
// Calculate expected frequency
double expectedFrequency = (size - handSize + 1) / (double)numHands;
double chiSquare = 0.0;
// Calculate Chi-Square statistic
for (int i = 0; i < numHands; i++) {
double observedFrequency = frequencies[i];
chiSquare += ((observedFrequency - expectedFrequency) *
(observedFrequency - expectedFrequency)) / expectedFrequency;
}
printf("Chi-Square Statistic: %.2f\n", chiSquare);
}
int main() {
int size;
int handSize;
// Input the size of the sequence
printf("Enter the number of elements in the sequence: ");
scanf("%d", &size);
if (size < 3) {
printf("Sequence must have at least 3 elements for three-digit test.\n");
return 1;
}
int data[size];
// Input the sequence of numbers
printf("Enter the sequence of numbers (digits only):\n");
for (int i = 0; i < size; i++) {
scanf("%1d", &data[i]);
}
// Input the type of Poker Test (3-digit or 4-digit)
printf("Enter 3 for three-digit Poker Test or 4 for four-digit Poker Test: ");
scanf("%d", &handSize);
if (handSize != 3 && handSize != 4) {
printf("Invalid input. Enter 3 or 4.\n");
return 1;
}
if (size < handSize) {
printf("Sequence must have at least %d elements for %d-digit test.\n", handSize,
handSize);
return 1;
}
Output:
// Perform the Poker Test
pokerTest(data, size, handSize);
return 0;
}
07. Perform Monte Carlo Simulation to estimate the circumference of a
circle (using c program and excel)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#defineRADIUS1.0 //Radiusofthecircle
#define NUM_POINTS 100000 // Number of random points
double random_double() {
return (double)rand() / RAND_MAX;
}
int main() {
int inside_circle = 0;
double x, y;
// Seed the random number generator
srand(time(NULL));
for (int i = 0; i < NUM_POINTS; i++) {
// Generate random points in the square [-1, 1] x [-1, 1]
x = 2.0 * random_double() - 1.0; y = 2.0 *
random_double() - 1.0;
// Check if the point is inside the circle
if (x * x + y * y <= RADIUS * RADIUS) {
inside_circle++;
}
}
// Calculate the estimated area of the circle
double estimated_area = (double)inside_circle / NUM_POINTS * 4.0;
// Calculate the circumference using the estimated area
double radius = RADIUS;
double estimated_circumference = 2 * M_PI * radius;
printf("Estimated Circumference: %f\n", estimated_circumference);
printf("Estimated Area: %f\n", estimated_area);
printf("Points Inside Circle: %d\n", inside_circle);
return 0;
}
Output:
IN EXCEL:
LAB 08: GPSS Program: Drive-thru facility of a restaurant:
We are assuming the following set of parameters to write a GPSS
simulation
program for Drive-thru model of Restaurant.
a. Customer arrival rate, from 3 to 5 minutes.
b. Time required to drive from main entrance to Drive-thru
window(facility) is 1 minute.
c. Order processing rate from 1 to 7 minutes per customer(order)-
uniformly distributed.
d. Perform Simulation for 250 customers.
Generate 4,1
Advance 1
Seize Windo
Advance 4,3
Release Windo
Terminate 1
Start 250
9. LAB 09. GPSS Program: Bank simulation:
a. Customer arrival rate is from 1 to 3 minutes.
b. Time required for a customer to move from entrance to the teller is 1
minute.
c. Customer has to stay on queue if teller is busy.
d. Teller processing rate is from 1 to 5 minutes per customer.
e. Perform simulation for 250 customers and for 60 minutes.
Generate 2,1
Advance 1
Queue Counter
Seize Teler
Depart Counter
Advance 3,2
Release Teler
Terminate 1
Start 250
If Simulation is asked for Some time units suppose for 60 minutes, then:
Generate 2,1
Advance 1
Queue Counter
Seize Teler
Depart Counter
Advance 3,2
Release Teler
Terminate 0
*Simulation Continues for 60 minutes*
Generate 60
Terminate 1
Start 1
10. LAB 10. GPSS Simulation:
Let us consider a bank in which all customers wait in a single line for a
free
teller rather than in individual line for each teller. There were 2 tellers for
initial 4 hours and then 3 tellers for next 4 hours. Customer arrival rate
was
from 1 to 3 minutes and customer need to walk for 1 minute to reach the
queue. Teller provides the service at the rate of 1 to 7 minutes for each
customer. Perform the simulation for 8 hours (2 tellers for initial 4 hours
and
then 3 tellers for next 4 hours) and compare different result.
Generate 2,1
Advance 1
Queue Windo
Enter Teler
Depart Windo
Advance 4,3
Leave Teler
Terminate 0
*Timing Section*
Generate 240
Terminate 1
*Define Storage*
Teler Storage 2
*Control Section*
Start 1
Clear
Teler Storage 3
Start 1