Repository navigation
Expand file tree
/
Copy pathHelper.cpp
More file actions
100 lines (85 loc) · 2.02 KB
/
Copy pathHelper.cpp
File metadata and controls
100 lines (85 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// Helper.cpp
// projestSMA
//
// Created by Tine Meersman on 09/02/2022.
// Copyright © 2022 Tine Meersman. All rights reserved.
//
#include "Helper.hpp"
#include <stdio.h>
#include <string>
#include <fstream>
#include <random>
double Exponential_distribution(double lambda){
float j1 = float (rand()%1000)/1000;
if (j1 == 0) j1 += 0.0001;
float j2 = -log(j1)/lambda;
return j2;
}
int Poisson_distribution(double lambda){
float k, L, j1, j2, j3;
int p;
j1 = float (rand()%1000)/1000;
k = 0;
L = exp(-lambda);
j3 = 0;
do{
j2 = L * pow(lambda, k);
p = 1;
for (int i6 = 0; i6 <= k; i6++){
if (i6 == 0)
p = 1;
else
p *= i6;
}
j2 /= p;
j3 += j2;
k++;
} while (j1 >= j3);
return k-1;
}
int Normal_distribution(double mean, double stdev){
// TO MODEL BASED ON CUMULATIVE DENSITY FUNCTION OF NORMAL DISTRIBUTION BASED ON BOOK OF SHELDON ROSS, Simulation, The polar method, p80.
float v1, v2, t;
int x;
do{
v1 = float (rand()%1000)*2;
v1 /= 1000;
v1 -= 1;
v2 = float (rand()%1000)*2;
v2 /= 1000;
v2 -= 1;
t=v1*v1+v2*v2;
}
while(t>=1||t==0);
float multiplier = sqrt(-2*log(t)/t);
x = v1 * multiplier * stdev + mean;
return x;
}
int Bernouilli_distribution(double prob){
float j1 = rand()%1000/1000;
if (j1 < prob)
return 0;
else
return 1;
}
int Uniform_distribution(double a, double b){
float j1 = rand()%1000/1000;
return (int) a + (b-a) * j1;
}
int Triangular_distribution(int a, int b, int c){
float mean, stdev,x, L;
mean = (a+b+c)/3;
stdev = (pow(a,2)+pow(b,2)+pow(c,2)-a*b-a*c-b*c)/18;
stdev = sqrt(stdev);
float j1 = float (rand()%1000)/1000;
x = a;
do{
if (x <= b)
L = pow((x-a),2)/((c-a)*(b-a));
else
L = 1-(pow(c-x,2)/((c-a)*(c-b)));
x++;
} while (j1 >= L);
return x-1;
}