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