eolib
0.5.0
A core C library for writing applications related to Endless Online
Loading...
Searching...
No Matches
rng.c
Go to the documentation of this file.
1
#include "
eolib/rng.h
"
2
3
static
uint32_t
state_high
;
4
static
uint32_t
current_seed
;
5
6
void
eo_srand
(uint32_t seed)
7
{
8
current_seed
= 0x1586B75 * seed + 1;
9
state_high
= 0;
10
}
11
12
uint32_t
eo_rand
()
13
{
14
uint32_t temp_state_product =
state_high
?
state_high
* 20021 : 0;
15
uint32_t temp_seed_product =
current_seed
* 346;
16
17
// multiply current_seed by 20021 using 64-bit integer
18
uint64_t full_product = (uint64_t)
current_seed
* 20021;
19
20
// extract high and low 32-bit parts
21
uint32_t seed_lo = (uint32_t)full_product;
22
uint32_t seed_hi = (uint32_t)(full_product >> 32);
23
24
// add contributions to high word
25
seed_hi += temp_state_product + temp_seed_product;
26
27
// increment the low word
28
seed_lo += 1;
29
30
// update state
31
current_seed
= seed_lo;
32
state_high
= seed_hi;
33
34
// return upper 31 bits
35
return
seed_hi & 0x7FFFFFFF;
36
}
37
38
uint32_t
eo_rand_range
(uint32_t min, uint32_t max)
39
{
40
if
(min > max)
41
{
42
return
min;
43
}
44
45
uint32_t range = max - min + 1;
46
return
min + (
eo_rand
() % range);
47
}
eo_rand
uint32_t eo_rand()
Definition
rng.c:12
state_high
static uint32_t state_high
Definition
rng.c:3
eo_srand
void eo_srand(uint32_t seed)
Definition
rng.c:6
eo_rand_range
uint32_t eo_rand_range(uint32_t min, uint32_t max)
Definition
rng.c:38
current_seed
static uint32_t current_seed
Definition
rng.c:4
rng.h
src
rng.c
Generated by
1.16.1