-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
64 lines (55 loc) · 1.75 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define CHARSET "abcdefghijklnmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define OFFSPRINGS_COUNT 10
#define GENOM_LENGTH 27
void generate_random_string(char *dest);
void mutate_random_gen(char *dest);
size_t generate_random_gen_index();
char get_random_char();
int main() {
srand(getpid());
char init_genom[GENOM_LENGTH], pre_genom[GENOM_LENGTH], next_genom[GENOM_LENGTH];
generate_random_string(init_genom);
printf("------------\n");
printf("Initial population: %s\n", init_genom);
printf("Genom length: %li\n", strlen(init_genom));
printf("Count of offsprings: %i\n", OFFSPRINGS_COUNT);
printf("------------\n");
printf("Offsprings:\n");
printf("------------\n");
strcpy(pre_genom, init_genom);
strcpy(next_genom, init_genom);
for (int i = 0; i < OFFSPRINGS_COUNT; i++) {
mutate_random_gen(next_genom);
printf("Population %i\n", i + 1);
printf("Ancestor genom: %s\n", init_genom);
printf("Previous genom: %s\n", pre_genom);
printf("Actual genom : %s\n", next_genom);
printf("------------\n");
memcpy(pre_genom, next_genom, GENOM_LENGTH);
}
printf("Mutation result\n");
printf("Initial genom : %s\n", init_genom);
printf("Final genom : %s\n", next_genom);
return 0;
}
void generate_random_string(char *dest) {
int l = GENOM_LENGTH - 1;
while (l --> 0) {
*dest++ = get_random_char();
}
*dest = '\0';
}
void mutate_random_gen(char *dest) {
size_t mutate_index = (double) rand() / RAND_MAX * (GENOM_LENGTH - 1);
dest[mutate_index] = get_random_char();
}
size_t generate_random_gen_index() {
return (double) rand() / RAND_MAX * (sizeof CHARSET - 1);
}
char get_random_char() {
return CHARSET[generate_random_gen_index()];
}