Debian Codegraph @

lib/util/rand.h

raw
1 /* Interface header file for random number generator functions in libnetpbm */
2
3 #ifndef RAND_H_INCLUDED
4 #define RAND_H_INCLUDED
5
6 #include <inttypes.h>
7
8 #include "netpbm/pm_c_util.h"
9 #include "netpbm/mallocvar.h"
10
11 /*
12 Definitions for selecting the random number generator
13
14 The default random number generator is Mersenne Twister. Here we
15 provide a means to revert to the system rand() generator if need be.
16
17 Glibc provides generators: rand(), random() and drand48(). Each has
18 its own associated functions. In the Glibc documentation rand() is
19 called "ISO", random() is called "BSD" and drand48() is called "SVID".
20 If your system has the glibc documentation installed "info rand" should
21 get you to the relevant page. The documentation is available online
22 from:
23
24 https://www.gnu.org/software/libc/manual/html_node/Pseudo_002dRandom-Numbers.html
25 Pseudo-Random Numbers (The GNU C Library)
26
27 Glibc's choice of name is confusing for what it calls "ISO" rand()
28 was available in early BSD systems.
29
30 Functions by these names appear on most Unix systems, but generation
31 formulas and default initial states are known to differ. On systems
32 which do not use glibc, what is called rand() may have no relation
33 with the formula of the ISO C standard. Likewise random() may have
34 no relation with the BSD formula.
35 */
36
37 enum PmRandEngine {PM_RAND_SYS_RAND, /* rand() */
38 PM_RAND_SYS_RANDOM, /* random() */
39 PM_RAND_SYS_DRAND48, /* drand48() reserved */
40 PM_RAND_MERSENNETWISTER /* default */};
41
42 #ifndef PM_RANDOM_NUMBER_GENERATOR
43 #define PM_RANDOM_NUMBER_GENERATOR PM_RAND_MERSENNETWISTER
44 #endif
45
46
47 /* Structure to hold random number generator profile and internal state */
48
49 struct pm_randSt;
50
51 struct pm_rand_vtable {
52 void
53 (*init)(struct pm_randSt * const randStP);
54
55 void
56 (*srand)(struct pm_randSt * const randStP,
57 unsigned int const seed);
58
59 unsigned long int
60 (*rand)(struct pm_randSt * const randStP);
61 };
62
63 extern struct pm_rand_vtable const pm_randsysrand_vtable;
64 extern struct pm_rand_vtable const pm_randsysrandom_vtable;
65 extern struct pm_rand_vtable const pm_randmersenne_vtable;
66
67 struct pm_randSt {
68 struct pm_rand_vtable vtable;
69 void * stateP; /* Internal state */
70 unsigned int max;
71 unsigned int seed;
72 bool gaussCacheValid;
73 double gaussCache;
74 };
75
76 /* Function declarations */
77
78 extern void
79 pm_randinit(struct pm_randSt * const randStP);
80
81 extern void
82 pm_randterm(struct pm_randSt * const randStP);
83
84 extern void
85 pm_srand(struct pm_randSt * const randStP,
86 unsigned int const seed);
87
88
89 extern void
90 pm_srand2(struct pm_randSt * const randStP,
91 bool const withSeed,
92 unsigned int const seedVal);
93
94 extern unsigned long int
95 pm_rand(struct pm_randSt * const randStP);
96
97 extern double
98 pm_drand(struct pm_randSt * const randStP);
99
100 extern double
101 pm_drand1(struct pm_randSt * const randStP);
102
103 extern double
104 pm_drand2(struct pm_randSt * const randStP);
105
106 extern void
107 pm_gaussrand2(struct pm_randSt * const randStP,
108 double * const r1P,
109 double * const r2P);
110
111 extern double
112 pm_gaussrand(struct pm_randSt * const randStP);
113
114 extern uint32_t
115 pm_rand32(struct pm_randSt * const randStP);
116
117
118 #endif