3 * $Id: lcrand.h,v 1.2 2000/06/17 11:28:51 mdw Exp $
5 * Simple linear congruential generator
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2000/06/17 11:28:51 mdw
34 * Amend the notes slightly.
36 * Revision 1.1 1999/12/10 23:15:27 mdw
37 * Noncryptographic random number generator.
41 /*----- Notes on the linear congruential generator ------------------------*
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
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.
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$%.
57 * The generator has been tested for its statistical properties. George
58 * Marsaglia's Diehard tests give it a reasonably clean bill of health.
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$%.
67 #ifndef CATACOMB_LCRAND_H
68 #define CATACOMB_LCRAND_H
74 /*----- Header files ------------------------------------------------------*/
76 #include <mLib/bits.h>
78 #ifndef CATACOMB_GRAND_H
82 /*----- Constants ---------------------------------------------------------*/
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 */
88 #define LCRAND_FIXEDPT 3223959250u /* Fixed point (only bad input) */
90 /*----- Functions provided ------------------------------------------------*/
94 * Arguments: @uint32 x@ = seed value
96 * Returns: New state of the generator.
98 * Use: Steps the generator. Returns %$ax + c \bmod p$%.
101 extern uint32
lcrand(uint32
/*x*/);
103 /* --- @lcrand_range@ --- *
105 * Arguments: @uint32 *x@ = pointer to seed value (updated)
106 * @uint32 m@ = limit allowable
108 * Returns: A uniformly distributed pseudorandom integer in the interval
112 extern uint32
lcrand_range(uint32 */
*x*/
, uint32
/*m*/);
114 /* --- @lcrand_create@ --- *
116 * Arguments: @uint32 x@ = initial seed
118 * Returns: Pointer to a generic generator.
120 * Use: Constructs a generic generator interface over a linear
121 * congruential generator.
124 extern grand
*lcrand_create(uint32
/*x*/);
126 /*----- That's all, folks -------------------------------------------------*/