-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_vectors.h
176 lines (156 loc) · 3.72 KB
/
gen_vectors.h
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
167
168
169
170
171
172
173
174
175
176
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>
#include<sys/stat.h>
#include<gmp.h>
#define MAX_FILE_NAME_LEN 256
const char PathSeparator =
#ifdef _WIN32
'\\';
#else
'/';
#endif
gmp_randstate_t state;
int state_initialized = 0;
int print_help( char *prog_name );
int initialize_random_state_and_seed(gmp_randstate_t state);
int generate_random_number( int k, mpz_t random_no );
int clear_random_state( gmp_randstate_t state );
int write_random_values_to_file(char *output_file_name, int no_bits, int no_dims);
int generate_urandom_number( int k, mpz_t random_no );
int initialize_random_state_and_seed(gmp_randstate_t state)
{
if ( state_initialized != 0 )
{
//already initialized
return 0;
}
gmp_randinit_mt( state );
#if DBG
fprintf(stdout, "%s:%d:: Should be called just once! state:%p\n", __func__, __LINE__, state);
#endif /*DBG*/
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long long millisecondsSinceEpoch =
(unsigned long long)(tv.tv_sec) * 1000 +
(unsigned long long)(tv.tv_usec) / 1000;
gmp_randseed_ui( state, (long) millisecondsSinceEpoch );
state_initialized = 1;
return 0;
}
int generate_random_number( int k, mpz_t random_no )
{
if ( k <= 0 )
{
fprintf(stderr, "k:%d\n", k);
return -1;
}
if ( random_no == NULL )
{
fprintf(stderr, "Bad arg. random_no:NULL\n");
return -2;
}
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p\n", __func__, __LINE__, state);
#endif /*DBG*/
if ( state_initialized == 0 )
{
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p\n", __func__, __LINE__, state);
#endif /*DBG*/
initialize_random_state_and_seed(state);
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p\n", __func__, __LINE__, state);
#endif /*DBG*/
}
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p, random_no:%p\n", __func__, __LINE__, state, random_no);
#endif /*DBG*/
mpz_rrandomb( random_no, state, k );
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p\n", __func__, __LINE__, state);
#endif /*DBG*/
return 0;
}
/*
Generates random number between 0 to 2^{k}-1
*/
int generate_urandom_number( int k, mpz_t random_no )
{
if ( k <= 0 )
{
fprintf(stderr, "k:%d\n", k);
return -1;
}
if ( random_no == NULL )
{
fprintf(stderr, "Bad arg. random_no:NULL\n");
return -2;
}
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p\n", __func__, __LINE__, state);
#endif /*DBG*/
if ( state_initialized == 0 )
{
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p\n", __func__, __LINE__, state);
#endif /*DBG*/
initialize_random_state_and_seed(state);
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p\n", __func__, __LINE__, state);
#endif /*DBG*/
}
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p, random_no:%p\n", __func__, __LINE__, state, random_no);
#endif /*DBG*/
mpz_urandomb( random_no, state, k );
#if DBG
fprintf(stdout, "%s:%d:: Debug! state:%p\n", __func__, __LINE__, state);
#endif /*DBG*/
return 0;
}
int write_random_values_to_file(char *output_file_name, int no_bits, int no_dims)
{
int err = 0, i;
mpz_t random_no;
FILE *fp = NULL;
if ( ( fp = fopen(output_file_name, "w") ) != NULL )
{
mpz_init(random_no);
for ( i=0; i<no_dims; i++ )
{
#if DBG
fprintf(stdout, "%s:%d:: Debug!\n", __func__, __LINE__);
#endif /*DBG*/
generate_random_number(no_bits, random_no);
#if DBG
fprintf(stdout, "%s:%d:: Debug!\n", __func__, __LINE__);
#endif /*DBG*/
if ( i < no_dims - 1 )
{
gmp_fprintf(fp, "%Zu\n", random_no);
}
else
{
gmp_fprintf(fp, "%Zu", random_no);
}
}
fclose(fp);
mpz_clear(random_no);
}
else
{
fprintf(stderr, "Cannot open file: %s\n", output_file_name);
return -1;
}
return err;
}
int clear_random_state( gmp_randstate_t state )
{
if ( state_initialized != 0 )
{
gmp_randclear( state );
}
return 0;
}