Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
randomkit.h
Go to the documentation of this file.
1/* Random kit 1.3 */
2
3/*
4 * Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org)
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
61#define RK_STATE_LEN 624
62
63typedef struct rk_state_
64{
65 unsigned long key[RK_STATE_LEN];
66 int pos;
67
68}
70
71/* Maximum generated random value */
72#define RK_MAX 0xFFFFFFFFUL
73
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78/*
79 * Initialize the RNG state using the given seed.
80 */
81extern void rk_seed(unsigned long seed, rk_state *state);
82
83/*
84 * Returns a random unsigned long between 0 and RK_MAX inclusive
85 */
86extern unsigned long rk_random(rk_state *state);
87
88/*
89 * Returns a random unsigned long between 0 and max inclusive.
90 */
91extern unsigned long rk_interval(unsigned long max, rk_state *state);
92
93#ifdef __cplusplus
94}
95#endif
static long seed
Definition exeval.c:1035
unsigned long rk_interval(unsigned long max, rk_state *state)
Definition randomkit.c:138
void rk_seed(unsigned long seed, rk_state *state)
Definition randomkit.c:75
struct rk_state_ rk_state
unsigned long rk_random(rk_state *state)
Definition randomkit.c:97
#define RK_STATE_LEN
Definition randomkit.h:61
unsigned long key[RK_STATE_LEN]
Definition randomkit.h:65