File: random.c

package info (click to toggle)
sameboy 1.0.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 10,528 kB
  • sloc: ansic: 29,948; objc: 22,249; asm: 1,424; pascal: 1,373; makefile: 1,065; xml: 111
file content (38 lines) | stat: -rw-r--r-- 581 bytes parent folder | download | duplicates (2)
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
#include "random.h"
#include <time.h>

static uint64_t seed;
static bool enabled = true;

uint8_t GB_random(void)
{
    if (!enabled) return 0;
    
    seed *= 0x27BB2EE687B0B0FDL;
    seed += 0xB504F32D;
    return seed >> 56;
}

uint32_t GB_random32(void)
{
    GB_random();
    return seed >> 32;
}

void GB_random_seed(uint64_t new_seed)
{
    seed = new_seed;
}

void GB_random_set_enabled(bool enable)
{
    enabled = enable;
}

static void __attribute__((constructor)) init_seed(void)
{
    seed = time(NULL);
    for (unsigned i = 64; i--;) {
        GB_random();
    }
}