eolib 0.6.1
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
5
6void eo_srand(uint32_t seed)
7{
8 current_seed = 0x1586B75 * seed + 1;
9 state_high = 0;
10}
11
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
39{
40 uint32_t range;
41
42 if (min > max)
43 {
44 return min;
45 }
46
47 range = max - min + 1;
48 return min + (eo_rand() % range);
49}
static uint32_t state_high
Definition rng.c:3
void eo_srand(uint32_t seed)
Definition rng.c:6
uint32_t eo_rand(void)
Definition rng.c:12
uint32_t eo_rand_range(uint32_t min, uint32_t max)
Definition rng.c:38
static uint32_t current_seed
Definition rng.c:4
unsigned int uint32_t
Definition stdint.h:80
unsigned __int64 uint64_t
Definition stdint.h:90