-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand.cpp
166 lines (126 loc) · 3.97 KB
/
rand.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "rand.h"
#include "main.h"
double *rand_bit;
double NUM_rand_bit;
void Bit_gen_memory_allocation(void)
{
rand_bit = (double *)malloc(NUM_rand_bit* sizeof(double));
}
void Rand_gen(void)
{
int i;
// int histo_i[100]={0};
// int j;
// FILE *fp_rand_bit;
/************************************************************
* data part generation
************************************************************/
for(i = 0 ; i < NUM_rand_bit ; i++) {
rand_bit[i] = (double)rand()/RAND_MAX; // information bit generation
/* // Histogram을 그리기 위한 part.
j = (int)(rand_bit[i]*100);
histo_i[j]++;
*/
// if(i<5) printf("[%d] %lf \n", i, rand_bit[i]);
}
/************************************************************
* Output
************************************************************/
// printf("%d \t%lf \n", 1, rand_bit[1]);
/* fp_rand_bit = fopen("rand_bit.txt", "w");
for(i = 0; i < NUM_rand_bit; i++) {
fprintf(fp_rand_bit, "%d \t%lf \n", i, rand_bit[i]);
// printf("[%d] %d \n", i, rand_bit[i]);
}
fprintf(fp_rand_bit, "\n\n\n HISTOGRAM Indicators \n");
for(i = 0; i < 100; i++) {
fprintf(fp_rand_bit, "%d \t%d \n", i, histo_i[i]);
// printf("[%d] %d \n", i, rand_bit[i]);
}
fclose(fp_rand_bit);
*/
}
void Exp_Rand_gen(double *exp_rand_bit, double Lambda)
{
int i;
// int j;
// double bin_dur;
// int histo_exp[100]={0};
// double exp_rand_MAX=0;
// FILE *fp_exp_rand_bit;
/************************************************************
* data part generation
************************************************************/
Rand_gen();
for(i = 0 ; i < NUM_rand_bit ; i++) {
// rand_bit가 0이면 무한대로 가버리는 에러가 생긴다.
if(rand_bit[i] == 0)
exp_rand_bit[i] = -(1./Lambda)*log(1-0.11111111);
else
exp_rand_bit[i] = -(1./Lambda)*log(1.-rand_bit[i]);
//exp_rand_MAX의 최대값을 구한다. histogram을 그리기 위한 tool이다.
/* if(exp_rand_MAX < exp_rand_bit[i])
exp_rand_MAX = exp_rand_bit[i];
*/
// if(i<5) printf("[%d] %lf \n", i, rand_bit[i]);
// if(i<5) printf("randb [%d] %lf \n", i, exp_rand_bit[i]);
}
// histogram을 위한 bin_duration
// bin_dur = exp_rand_MAX/100;
/* // histogram을 그리는 함수
for(i = 0 ; i < NUM_rand_bit ; i++) {
j = (int)(exp_rand_bit[i]*(1/bin_dur));
histo_exp[j]++;
}
*/
// printf("exp_rand_MAX = %lf \n", 1, exp_rand_MAX);
/********************************************************
* Output
********************************************************/
/*
fp_exp_rand_bit = fopen("exp_rand_bit.txt", "w");
for(i = 0; i < NUM_rand_bit; i++) {
fprintf(fp_exp_rand_bit, "%d \t%lf \n", i, exp_rand_bit[i]);
}
fprintf(fp_exp_rand_bit, "\n\n\n HISTOGRAM Indicators \n");
for(i = 0; i < 100; i++) {
fprintf(fp_exp_rand_bit, "%d \t%d \n", i, histo_exp[i]);
}
fclose(fp_exp_rand_bit);
*/
}
double Exp_Rand_gen1(double Lambda)
{
double exp_rand_bit;
/************************************************************
* data part generation
************************************************************/
Rand_gen();
// rand_bit가 0이면 무한대로 가버리는 에러가 생긴다.
if(rand_bit[0] == 0)
exp_rand_bit = -(1./Lambda)*log(1-0.11111111);
else if(rand_bit[0] ==1)
exp_rand_bit = 0;
else
exp_rand_bit = -(1./Lambda)*log(1.-rand_bit[0]);
return exp_rand_bit;
}
/********************************************************
* Memory_free, Initialization, etc.
********************************************************/
void Bit_gen(int * bit_stream, int stream_size)
{
int i;
for(i=0; i<stream_size; i++)
{
bit_stream[i] = rand()%2;
}
}
void Bit_gen_memory_free(void)
{
free(rand_bit);
}