-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsg.c
57 lines (54 loc) · 1.08 KB
/
rsg.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
#include "rsg.h"
#include "lcg.c"
#include <fcntl.h>
#include <png.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
int *fy_shuffle(Lcg *s) {
int temp, rnd;
int *arr = malloc(s->m * sizeof(int));
for (int i = 0; i < s->m; i++) {
arr[i] = i;
}
for (int i = s->m - 1; i > 0; i--) {
s->m = i;
rnd = generate_single(s);
// printf("%d\n", rnd);
temp = arr[i];
arr[i] = arr[rnd];
arr[rnd] = temp;
}
return arr;
}
unsigned long int hash(char *key) {
//
unsigned long hash = 5381;
int c;
while ((c = *key++)) {
hash = ((hash << 5) + hash) + c;
}
return hash;
}
Lcg *init_seed(char *key, int range) {
unsigned long int hashed_key = hash(key);
Lcg *s = (Lcg *)malloc(sizeof(Lcg));
s->seed = hashed_key;
s->current = hashed_key;
s->a = 987654321;
s->c = 33;
s->m = range;
return s;
}
// int main() {
//
// char *key = "heyyyy";
// Lcg *t = init_seed(key);
// t->m = 10;
// int *arr = fy_shuffle(t);
// printf("-----\n");
// for (int i = 0; i < 10; i++) {
// printf("%d\n", arr[i]);
// }
// return 0;
// }