Graphviz 13.1.2~dev.20250725.0048
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#if !defined(__CYGWIN__) && defined(__GNUC__) && !defined(__MINGW32__)
62#define INTERNAL __attribute__((visibility("hidden")))
63#else
64#define INTERNAL /* nothing */
65#endif
66
67#define RK_STATE_LEN 624
68
69typedef struct rk_state_
70{
71 unsigned long key[RK_STATE_LEN];
72 int pos;
73
74}
76
77/* Maximum generated random value */
78#define RK_MAX 0xFFFFFFFFUL
79
80#ifdef __cplusplus
81extern "C" {
82#endif
83
84/*
85 * Initialize the RNG state using the given seed.
86 */
87INTERNAL void rk_seed(unsigned long seed, rk_state *state);
88
89/*
90 * Returns a random unsigned long between 0 and RK_MAX inclusive
91 */
92INTERNAL unsigned long rk_random(rk_state *state);
93
94/*
95 * Returns a random unsigned long between 0 and max inclusive.
96 */
97INTERNAL unsigned long rk_interval(unsigned long max, rk_state *state);
98
99#ifdef __cplusplus
100}
101#endif
102
103#undef INTERNAL
static long seed
Definition exeval.c:1053
#define INTERNAL
Definition randomkit.h:64
INTERNAL void rk_seed(unsigned long seed, rk_state *state)
Definition randomkit.c:75
INTERNAL unsigned long rk_random(rk_state *state)
Definition randomkit.c:97
struct rk_state_ rk_state
INTERNAL unsigned long rk_interval(unsigned long max, rk_state *state)
Definition randomkit.c:138
#define RK_STATE_LEN
Definition randomkit.h:67
unsigned long key[RK_STATE_LEN]
Definition randomkit.h:71