Skip to content

Commit 37cb9ac

Browse files
committed
feat: add generate random funtion
1 parent 719fa71 commit 37cb9ac

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

binary_code.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ void calculate_minimum_hamming_distance(HammingCode *code) {
5858
code->minimum_hamming_distance = distMin;
5959
}
6060

61+
short int *generate_random_word(int code_order) {
62+
short int *new_word = (short int *) calloc(code_order, sizeof(short int));
63+
for (int j = 0; j < code_order; j++){
64+
new_word[j] = (rand() % 2);
65+
}
66+
return new_word;
67+
}
6168

6269
HammingCode generate_code(int order, int length) {
6370

@@ -73,13 +80,7 @@ HammingCode generate_code(int order, int length) {
7380

7481

7582
for (int i = 0; i < length; i++) {
76-
short int *new_word = NULL;
77-
new_word = (short int *) calloc(code_order, sizeof(short int));
78-
79-
80-
// Generate new random value
81-
for (int j = 0; j < code_order; j++)
82-
new_word[j] = (rand() % 2);
83+
short int *new_word = generate_random_word(order);
8384

8485
while (code != NULL) {
8586
// Calculate hamming distance
@@ -97,13 +98,13 @@ HammingCode generate_code(int order, int length) {
9798
code = code->next;
9899
}
99100

100-
c->code = append_new_word(c->code, new_word); // adiciona palavra ao codigo;
101+
c->code = append_new_word(c->code, new_word);
101102
c->length++;
102103

103104

104105
}
105106

106-
calculate_minimum_hamming_distance(c); // calcula nova distancia minima
107+
calculate_minimum_hamming_distance(c);
107108

108109
return *c;
109110
}

0 commit comments

Comments
 (0)