07871354 |
1 | /* -*-c-*- |
07871354 |
2 | * |
3 | * Simple linear congruential generator |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
07871354 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
12 | * Catacomb is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the |
15 | * License, or (at your option) any later version. |
45c0fd36 |
16 | * |
07871354 |
17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU Library General Public License for more details. |
45c0fd36 |
21 | * |
07871354 |
22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
07871354 |
28 | /*----- Notes on the linear congruential generator ------------------------* |
29 | * |
30 | * This pseudorandom number generator is simple, but has absolutely no |
31 | * cryptographic strength whatever. It may be used whenever random numbers |
32 | * are required but cryptographic strength is not, for example when |
33 | * generating numbers for use in primality tests. To be honest, it's not |
34 | * even particularly fast, although a certain amount of effort has been |
bc4c10df |
35 | * expended on making it better than awfully slow. To put things in |
36 | * perspective, it can't quite spit bytes out as fast as OFB DES. (Then |
37 | * again, bytes aren't its natural output format.) Its main use is probably |
38 | * seeding a Fibonacci generator. |
07871354 |
39 | * |
40 | * There exists a fixed-point input @LCRAND_FIXEDPT@ -- when fed to the |
41 | * generator it comes straight back out again. All other inputs less than |
42 | * the modulus are part of the same sequence of period %$p - 1$%. |
43 | * |
44 | * The generator has been tested for its statistical properties. George |
45 | * Marsaglia's Diehard tests give it a reasonably clean bill of health. |
46 | * |
47 | * The modulus %$p$% is chosen as the largest prime number less than |
48 | * %$2^{32}$%. The multiplier %$a$% and additive constant %$c$% are based on |
49 | * the decimal expansions of %$\pi$% and %$e$%, with the additional |
50 | * restriction that the multiplier must be a primitive element modulo %$p$%. |
51 | * The fixed point value is determined as %$c / (1 - a) \bmod p$%. |
52 | */ |
53 | |
bc4c10df |
54 | #ifndef CATACOMB_LCRAND_H |
55 | #define CATACOMB_LCRAND_H |
56 | |
57 | #ifdef __cplusplus |
58 | extern "C" { |
59 | #endif |
60 | |
07871354 |
61 | /*----- Header files ------------------------------------------------------*/ |
62 | |
63 | #include <mLib/bits.h> |
64 | |
65 | #ifndef CATACOMB_GRAND_H |
66 | # include "grand.h" |
67 | #endif |
68 | |
69 | /*----- Constants ---------------------------------------------------------*/ |
70 | |
71 | #define LCRAND_P 4294967291u /* Modulus for the generator */ |
72 | #define LCRAND_A 314159265u /* Multiplier (primitive mod @p@) */ |
73 | #define LCRAND_C 271828183u /* Additive constant */ |
74 | |
75 | #define LCRAND_FIXEDPT 3223959250u /* Fixed point (only bad input) */ |
76 | |
77 | /*----- Functions provided ------------------------------------------------*/ |
78 | |
79 | /* --- @lcrand@ --- * |
80 | * |
81 | * Arguments: @uint32 x@ = seed value |
82 | * |
83 | * Returns: New state of the generator. |
84 | * |
85 | * Use: Steps the generator. Returns %$ax + c \bmod p$%. |
86 | */ |
87 | |
88 | extern uint32 lcrand(uint32 /*x*/); |
89 | |
90 | /* --- @lcrand_range@ --- * |
91 | * |
92 | * Arguments: @uint32 *x@ = pointer to seed value (updated) |
93 | * @uint32 m@ = limit allowable |
94 | * |
95 | * Returns: A uniformly distributed pseudorandom integer in the interval |
96 | * %$[0, m)$%. |
97 | */ |
98 | |
99 | extern uint32 lcrand_range(uint32 */*x*/, uint32 /*m*/); |
100 | |
101 | /* --- @lcrand_create@ --- * |
102 | * |
103 | * Arguments: @uint32 x@ = initial seed |
104 | * |
105 | * Returns: Pointer to a generic generator. |
106 | * |
107 | * Use: Constructs a generic generator interface over a linear |
108 | * congruential generator. |
109 | */ |
110 | |
111 | extern grand *lcrand_create(uint32 /*x*/); |
112 | |
113 | /*----- That's all, folks -------------------------------------------------*/ |
114 | |
115 | #ifdef __cplusplus |
116 | } |
117 | #endif |
118 | |
119 | #endif |