-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.c
46 lines (37 loc) · 1.03 KB
/
random.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void random_name_generator(char *str, uint32_t size);
int main(int argc, char **argv) {
srand(time(NULL));
if (argc < 3) {
printf("Usage <prog> <string-length> <count>\n");
return EXIT_FAILURE;
}
uint32_t size = atoi(argv[1]);
uint32_t count = atoi(argv[2]);
int i = 0;
while (i < count) {
char *r1 = malloc(sizeof(*r1) * size);
random_name_generator(r1, size);
printf("%s\n", r1);
free(r1);
r1 = NULL;
i += 1;
}
return EXIT_SUCCESS;
}
void random_name_generator(char *str, uint32_t size) {
if (size && str) {
const char *charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const int8_t charset_size = strlen(charset);
int8_t i = 0;
for (i = 0; i < size - 1; i++) {
int8_t index = rand() % charset_size;
str[i] = charset[index];
}
str[i] = 0;
}
}