Debian Codegraph @

lib/neatogen/randomkit.h

raw
1 /* Random kit 1.3 */
2
3 /*
4 * Copyright (c) 2003-2005, Jean-Sebastien Roy ([email protected])
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /*
27 * Typical use:
28 *
29 * {
30 * rk_state state;
31 * unsigned long seed = 1, random_value;
32 *
33 * rk_seed(seed, &state); // Initialize the RNG
34 * ...
35 * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
36 * }
37 *
38 * Instead of rk_seed, you can use rk_randomseed which will get a random seed
39 * from /dev/urandom (or the clock, if /dev/urandom is unavailable):
40 *
41 * {
42 * rk_state state;
43 * unsigned long random_value;
44 *
45 * rk_randomseed(&state); // Initialize the RNG with a random seed
46 * ...
47 * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
48 * }
49 */
50
51 /*
52 * Useful macro:
53 * RK_DEV_RANDOM: the device used for random seeding.
54 * defaults to "/dev/urandom"
55 */
56
57 #pragma once
58
59 #include <stddef.h>
60 #include <util/api.h>
61
62 #define RK_STATE_LEN 624
63
64 typedef struct rk_state_
65 {
66 unsigned long key[RK_STATE_LEN];
67 int pos;
68
69 }
70 rk_state;
71
72 /* Maximum generated random value */
73 #define RK_MAX 0xFFFFFFFFUL
74
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78
79 /*
80 * Initialize the RNG state using the given seed.
81 */
82 PRIVATE void rk_seed(unsigned long seed, rk_state *state);
83
84 /*
85 * Returns a random unsigned long between 0 and RK_MAX inclusive
86 */
87 PRIVATE unsigned long rk_random(rk_state *state);
88
89 /*
90 * Returns a random unsigned long between 0 and max inclusive.
91 */
92 PRIVATE unsigned long rk_interval(unsigned long max, rk_state *state);
93
94 #ifdef __cplusplus
95 }
96 #endif