-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistribute.h
More file actions
80 lines (65 loc) · 1.78 KB
/
distribute.h
File metadata and controls
80 lines (65 loc) · 1.78 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
#ifndef DISTRIBUTE
#define DISTRIBUTE
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define STR_SIZE 20
// Declarations
int generate_priority(int minPrio, int maxPrio);
int generate_process_length(char distPL[STR_SIZE], int avgPL, int minPL, int maxPL);
int generate_interarrival_time(char distIAT[STR_SIZE], int avgIAT, int minIAT, int maxIAT);
// Implementation
int generate_priority(int minPrio, int maxPrio)
{
int num = (rand() % (maxPrio - minPrio + 1)) + minPrio;
return num;
}
int generate_process_length(char distPL[STR_SIZE], int avgPL, int minPL, int maxPL)
{
if ( strcmp(distPL, "fixed") == 0 )
{
return avgPL;
}
else if ( strcmp(distPL, "uniform") == 0 )
{
int length = (rand() % (maxPL - minPL + 1)) + minPL;
return length;
}
else if ( strcmp(distPL, "exponential") == 0 )
{
double lambda = (double) 1 / avgPL;
double x;
do
{
double u = (double)rand() / (double)RAND_MAX;
x = ((-1) * log(1 - u)) / lambda;
} while ( minPL > x || x > maxPL );
return (int) x;
}
return -1;
}
int generate_interarrival_time(char distIAT[STR_SIZE], int avgIAT, int minIAT, int maxIAT)
{
if ( strcmp(distIAT, "fixed") == 0 )
{
return avgIAT;
}
else if ( strcmp(distIAT, "uniform") == 0 )
{
int length = (rand() % (maxIAT - minIAT + 1)) + minIAT;
return length;
}
else if ( strcmp(distIAT, "exponential") == 0 )
{
double lambda = (double) 1 / avgIAT;
double x;
do
{
double u = (double)rand() / (double)RAND_MAX;
x = ((-1) * log(1 - u)) / lambda;
} while ( minIAT > x || x > maxIAT );
return (int) x;
}
return -1;
}
#endif