-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandom.hpp
More file actions
74 lines (63 loc) · 2.02 KB
/
random.hpp
File metadata and controls
74 lines (63 loc) · 2.02 KB
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
65
66
67
68
69
70
71
72
73
74
#include "32blit.hpp"
// This stuff is all totally random
enum RANDOM_SOURCE {
PRNG, // Psuedo random, uses a linear-feedback shift register
HRNG // Really very random, uses the cryptographically awesome hardware RNG
};
RANDOM_SOURCE current_random_source = RANDOM_SOURCE::PRNG;
uint32_t current_random_seed = 0x64063701; // G A D G E T O I DOH!
uint32_t prng_lfsr = current_random_seed;
const uint16_t prng_tap = 0x74b8; // Magic number, do not drink
void random_reset() {
prng_lfsr = current_random_seed;
}
uint32_t get_random_int() {
switch(current_random_source) {
default:
return 0;
case HRNG:
// Chosen by fair dice roll, guranteed to be random
return blit::random();
break;
case PRNG:
// Bruteforce a new random number within the given range
while(1) {
uint32_t r = prng_lfsr;
uint8_t lsb = prng_lfsr & 1;
prng_lfsr >>= 1;
if (lsb) {
prng_lfsr ^= prng_tap;
}
return r;
}
break;
}
}
blit::Point get_random_point(blit::Size within) {
switch(current_random_source) {
default:
return blit::Point(0, 0);
case HRNG:
// Chosen by fair dice roll, guranteed to be random
return blit::Point(
blit::random() % within.w,
blit::random() % within.h
);
break;
case PRNG:
// Bruteforce a new random number within the given range
while(1) {
uint16_t x = prng_lfsr & 0x00ff;
uint16_t y = (prng_lfsr & 0x7f00) >> 8;
uint8_t lsb = prng_lfsr & 1;
prng_lfsr >>= 1;
if (lsb) {
prng_lfsr ^= prng_tap;
}
if (x < within.w && y < within.h) {
return blit::Point(x, y);
}
}
break;
}
}