-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomval.c
279 lines (181 loc) · 6.82 KB
/
randomval.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "randomval.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static long int randomSeed = 0;
static short int randomGeneratorInitialized = 0;
long int initializeRandomGenerator(int* seed, short int randGenInit){
randomSeed = (seed == NULL) ? time(NULL) : *seed;
randomGeneratorInitialized = (randGenInit == 1 || randGenInit == 2) ? randGenInit : randomGeneratorInitialized;
if(randomGeneratorInitialized != 1) {
srand(randomSeed);
randomGeneratorInitialized = (randomGeneratorInitialized == 0) ? 1 : 2;
}
return randomSeed;
}
// Generates a random integer in the interval from start to end (excluded
// with values' difference equal to step
IntType randInt(IntType start, IntType end, IntType step){
initializeRandomGenerator(NULL, 0);
if(end < start || step <= 0) return 0; // Avoid invalid values
IntType diff = ( (end - start) / step ) + 1; // How many values are present in the interval
return start + (rand() % diff ) * step; // This way the values satisfy the given conditions
}
// Fills an array of len dimension with random values
// in the interval [start; end) and separated by step
void randIntArr(IntType* arr, unsigned long int len, IntType start, IntType end, IntType step){
unsigned long int i;
for(i = 0; i < len; i++)
arr[i] = randInt(start, end, step);
}
// Fills an array of len dimension with values
// from start (included), separated by step
void genIntSeq(IntType* arr, unsigned long int len, IntType start, IntType step){
unsigned long int i;
for(i = 0; i < len; i++)
arr[i] = start + i*step;
}
// Swaps two variable values
void swapInt(IntType* a, IntType* b){
IntType temp = *a;
*a = *b;
*b = temp;
}
// Reverses the order of elements in an array
void reverseIntArr(IntType* arr, unsigned long len){
unsigned long int i;
for(i = 0; i < len/2; i++)
swapInt(arr+i, arr+(len - 1 - i));
}
void randIntSort(IntType *arr, unsigned long int len, unsigned long int nperm){
unsigned long int choice1, choice2;
unsigned long int i;
for(i = 0; i < nperm; i++){
choice1 = rand() % len;
choice2 = rand() % len;
swapInt(arr+choice1, arr+choice2);
}
}
// Fills an array with a random sequence of unique values
// beginning from start and separated by step value
void randIntSeq(IntType* arr, unsigned long int len, IntType start, IntType step){
genIntSeq(arr, len, start, step);
randIntSort(arr, len, NPERM);
}
void randIntMatrix(IntType* mat, unsigned long int row, unsigned long int col, IntType start, IntType end, IntType step){
unsigned long int i, j;
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
*(mat+i*col+j) = randInt(start, end, step);
}
}
}
void printIntMatrix(IntType* mat, unsigned long int row, unsigned long int col, char* format){
unsigned long int i, j;
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
printf(format, *(mat+i*col+j));
}
printf("\n");
}
}
void printIntArr(IntType* arr, unsigned long int len, char* format){
//printf("{");
for(unsigned long int i = 0; i < len; i++){
printf(format, *(arr+i));
}
//if(len != 0) printf("\b\b");
//printf("}\n");
printf("\n");
}
FracType randFrac(FracType start, FracType end, FracType step){
initializeRandomGenerator(NULL, 0);
if(end < start || step <= 0) return 0; // Avoid invalid values
long int diff = ( (end - start) / step ) + 1; // How many values are present in the interval
return start + (rand() % diff) * step; // This way the values satisfy the given conditions
}
// Fills an array of len dimension with random values
// in the interval [start; end) and separated by step
void randFracArr(FracType* arr, unsigned long int len, FracType start, FracType end, FracType step){
unsigned long int i;
for(i = 0; i < len; i++)
arr[i] = randFrac(start, end, step);
}
// Fills an array of len dimension with values
// from start (included), separated by step
void genFracSeq(FracType* arr, unsigned long int len, FracType start, FracType step){
long unsigned int i;
for(i = 0; i < len; i++)
arr[i] = start + i*step;
}
// Swaps two variable values
void swapFrac(FracType* a, FracType* b){
FracType temp = *a;
*a = *b;
*b = temp;
}
// Reverses the order of elements in an array
void reverseFracArr(FracType* arr, unsigned long int len){
unsigned long int i;
for(i = 0; i < len/2; i++)
swapFrac(arr+i, arr+(len - 1 - i));
}
void randFracSort(FracType *arr, unsigned long int len, IntType nperm){
IntType choice1, choice2;
IntType i;
for(i = 0; i < nperm; i++){
choice1 = rand() % len;
choice2 = rand() % len;
swapFrac(arr+choice1, arr+choice2);
}
}
// Fills an array with a random sequence of unique values
// beginning from start and separated by step value
void randFracSeq(FracType* arr, unsigned long int len, FracType start, FracType step){
genFracSeq(arr, len, start, step);
randFracSort(arr, len, NPERM);
}
void randFracMatrix(FracType* mat, unsigned long int row, unsigned long int col, FracType start, FracType end, FracType step){
unsigned long int i, j;
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
*(mat+i*col+j) = randFrac(start, end, step);
}
}
}
void printFracArr(FracType* arr, unsigned long int len, char* format){
//printf("{");
for(unsigned long int i = 0; i < len; i++){
printf(format, arr[i]);
}
//if(len != 0) printf("\b\b");
//printf("}\n");
printf("\n");
}
void printFracMatrix(FracType* mat, unsigned long int row, unsigned long int col, char* format){
unsigned long int i, j;
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
printf(format, *(mat+i*col+j));
}
printf("\n");
}
}
char randChar(char type){
static char alpha[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char symbols[32] = "\\`\"#%'&()[]{}*+-/.;:!?<>=^|$@~_";
switch(type){
case 'l': return randInt('a', 'z', 1); // Lowercase
case 'u': return randInt('A', 'Z', 1); // Uppercase
case 'a': return alpha[randInt(0, 51, 1)]; // Alphabetic
case 'n': return randInt('0', '9', 1); // Number
case 's': return symbols[randInt(0, 30, 1)]; // Symbols
case 'A': return randInt(32, 126, 1); // All
default : return '\0';
}
}
void randStr(char* str, char* mask){
for(unsigned long int i = 0; mask[i] != '\0'; i++)
str[i] = randChar(mask[i]);
}