-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.c
50 lines (46 loc) · 1.15 KB
/
producer.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
int main(int argc, char const *argv[])
{
// initialize random
srand(time(NULL));
int m = 1;
// check for arguments
if (argc < 2)
{
printf(" ! Not enough arguments are given! (expected 1, given %d)\n", argc - 1);
printf(" continuing with default values b=1 m=1\n");
exit(-1);
}
else
{
m = atoi(argv[1]); // expected positive
}
for (size_t i = 0; i < m; i++)
{
int gen = rand() % 3;
if (gen == 0) // generate number char
{
int r = (rand() % (57 - 48 + 1)) + 48;
printf("%c", (char) r);
}
else if (gen == 1) // generate uppercase letter
{
int r = (rand() % (65 - 90 + 1)) + 65;
printf("%c", (char) r);
}
else if (gen == 2) // generate lowercase letter
{
int r = (rand() % (97 - 122 + 1)) + 97;
printf("%c", (char) r);
}
}
return 0;
}