3 * Simple linear congruential generator
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Notes on the linear congruential generator ------------------------*
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
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.
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$%.
44 * The generator has been tested for its statistical properties. George
45 * Marsaglia's Diehard tests give it a reasonably clean bill of health.
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$%.
54 #ifndef CATACOMB_LCRAND_H
55 #define CATACOMB_LCRAND_H
61 /*----- Header files ------------------------------------------------------*/
63 #include <mLib/bits.h>
65 #ifndef CATACOMB_GRAND_H
69 /*----- Constants ---------------------------------------------------------*/
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 */
75 #define LCRAND_FIXEDPT 3223959250u /* Fixed point (only bad input) */
77 /*----- Functions provided ------------------------------------------------*/
81 * Arguments: @uint32 x@ = seed value
83 * Returns: New state of the generator.
85 * Use: Steps the generator. Returns %$ax + c \bmod p$%.
88 extern uint32 lcrand(uint32 /*x*/);
90 /* --- @lcrand_range@ --- *
92 * Arguments: @uint32 *x@ = pointer to seed value (updated)
93 * @uint32 m@ = limit allowable
95 * Returns: A uniformly distributed pseudorandom integer in the interval
99 extern uint32 lcrand_range(uint32 */*x*/, uint32 /*m*/);
101 /* --- @lcrand_create@ --- *
103 * Arguments: @uint32 x@ = initial seed
105 * Returns: Pointer to a generic generator.
107 * Use: Constructs a generic generator interface over a linear
108 * congruential generator.
111 extern grand *lcrand_create(uint32 /*x*/);
113 /*----- That's all, folks -------------------------------------------------*/