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

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

#define AL_RAND_MAX RAND_MAX

extern u32 al_rand_seed;
extern __thread bool al_rand_set;

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

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

#endif // _AL_RANDOM_H