summaryrefslogtreecommitdiff
path: root/include/al/random.h
blob: 3d0d7cb845723bfacb4dcf3ac86b18e8a92cdd4d (plain)
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
#ifndef _AL_RANDOM_H
#define _AL_RANDOM_H

#include "lib.h"
#include "macros.h"

#define AL_RAND_MAX RAND_MAX

AL_UNUSED_FUNCTION_PUSH

extern u32 al_rand_seed;
extern __thread bool al_rand_set;

static void al_rand_init(u32 seed)
{
    al_rand_seed = seed;
}

static s32 al_rand(void)
{
    if (UNLIKELY(!al_rand_set)) {
        // srand() is thread-local on Windows CRT.
        srand(al_rand_seed);
        al_rand_set = true;
    }
    return rand();
}

AL_UNUSED_FUNCTION_POP

#endif // _AL_RANDOM_H