summaryrefslogtreecommitdiff
path: root/lib/rnd.h
blob: 627b24d8fa6b0756c46b5867e2db6ac65b7cde40 (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
32
33
34
35
36
37
38
#ifndef lib_rnd_h
#define lib_rnd_h
/* Maria project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/rnd.h
 * \brief   Random Number Generator header.
 * \ingroup lib
 */

/** Random number generator context. */
struct lib_rnd_t
{
#define LIB_MT_N 624
    unsigned long state[LIB_MT_N];
    int state_index;
};
typedef struct lib_rnd_t lib_rnd_t;

/** Generate a integer which has a probability of \a prob to be greater than a
 * random 32 bit integer. */
#define LIB_RND_RATIO(prob) ((u32) ((1ull << 32) * (prob)))

void
lib_rnd_init (lib_rnd_t *ctx, u32 seed);
void
lib_rnd_init_by_array (lib_rnd_t *ctx, const u32 init_key[], int key_length);
u32
lib_rnd32 (lib_rnd_t *ctx);
uint
lib_rnd_uniform (lib_rnd_t *ctx, uint bound);

#endif /* lib_rnd_h */