-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreer_vecteur.c
65 lines (58 loc) · 1.87 KB
/
creer_vecteur.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
void usage(char *commande) {
fprintf(stderr, "Usage :\n");
fprintf(stderr, "%s [ --seed number ] [ --size number ] [ --min value ] "
"[ --max value ] [ --help ]\n\n", commande);
fprintf(stderr, "Ce programme affiche sur sa sortie standard la taille "
"donnee (defaut 1000) puis autant d'entiers que cette "
"taille. Chacun de ces entiers est genere aleatoirement "
"dans l'intervalle [min;max] (defaut [0;1000]). Le "
"generateur aleatoire est initialise avec la valeur "
"donnee pour seed ou, a defaut, avec l'horloge systeme.\n");
exit(1);
}
int main(int argc, char *argv[]) {
int graine, taille, min, max, i, opt;
struct option longopts[] = {
{ "help", required_argument, NULL, 'h' },
{ "seed", required_argument, NULL, 'S' },
{ "size", required_argument, NULL, 's' },
{ "min", required_argument, NULL, 'm' },
{ "max", required_argument, NULL, 'M' },
{ NULL, 0, NULL, 0 }
};
graine = time(NULL);
taille = 1000;
min = 0;
max = 1000;
while ((opt = getopt_long(argc, argv, "hs:S:m:M:", longopts, NULL)) != -1) {
switch (opt) {
case 'm':
min = atoi(optarg);
break;
case 'M':
max = atoi(optarg);
break;
case 's':
taille = atoi(optarg);
break;
case 'S':
graine = atoi(optarg);
break;
case 'h':
default:
usage(argv[0]);
}
}
argc -= optind;
argv += optind;
srandom(graine);
printf("%d\n", taille);
for (i=0; i<taille; i++)
printf("%ld\n", (random()%(max-min) + min));
return 0;
}