-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenData.c
77 lines (65 loc) · 1.46 KB
/
GenData.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
#include <stdio.h>
#include <stdlib.h>
typedef int T_Seed;
#define MY_DEFAULT_SEED 2024
#define SeedTypeTrans(S) atoi(S)
#define RandInit(S) srand(S)
#define Rand(I) ((double)rand() / ((double)RAND_MAX + 1.0) * 4.0)
void usage(char *);
void generate(int, char *, T_Seed, int);
void usage(char *cmd) {
fprintf(stderr, "Usage: %s [-n] [-s seed] nitem outfile\n", cmd);
exit(1);
}
int main(int argc, char *argv[]) {
char *cmd, *fname = NULL;
int flag_nitem = 0, nitem = 0;
T_Seed seed = MY_DEFAULT_SEED;
cmd = *argv++;
while (--argc > 0) {
if ((*argv)[0] != '-')
break;
switch ((*argv)[1]) {
case 'n':
flag_nitem = 1;
break;
case 's':
seed = SeedTypeTrans(*++argv);
argc--;
break;
default:
usage(cmd);
break;
}
argv++;
}
if (argc == 2) {
if ((nitem = atoi(argv[0])) <= 0) {
fprintf(stderr, "Illegal number of items.\n");
usage(cmd);
}
fname = argv[1];
} else {
usage(cmd);
}
generate(nitem, fname, seed, flag_nitem);
return 0;
}
void generate(int nitem, char *fname, T_Seed seed, int nflag) {
FILE *fp;
double r;
int i;
if ((fp = fopen(fname, "w")) == NULL) {
fprintf(stderr, "Unable to open %s\n", fname);
exit(1);
}
if (nflag) {
fprintf(fp, "%d\n", nitem);
}
RandInit(seed);
for (i = 0; i < nitem; i++) {
r = Rand(i);
fprintf(fp, "%f\t%d\tMake_Aquatan_Great_Again_%f\n", r, nitem - i, 5.0 - r);
}
fclose(fp);
}