3 * $Id: lcrand.h,v 1.3 2004/04/08 01:36:15 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 /*----- Notes on the linear congruential generator ------------------------*
32 * This pseudorandom number generator is simple, but has absolutely no
33 * cryptographic strength whatever. It may be used whenever random numbers
34 * are required but cryptographic strength is not, for example when
35 * generating numbers for use in primality tests. To be honest, it's not
36 * even particularly fast, although a certain amount of effort has been
37 * expended on making it better than awfully slow. To put things in
38 * perspective, it can't quite spit bytes out as fast as OFB DES. (Then
39 * again, bytes aren't its natural output format.) Its main use is probably
40 * seeding a Fibonacci generator.
42 * There exists a fixed-point input @LCRAND_FIXEDPT@ -- when fed to the
43 * generator it comes straight back out again. All other inputs less than
44 * the modulus are part of the same sequence of period %$p - 1$%.
46 * The generator has been tested for its statistical properties. George
47 * Marsaglia's Diehard tests give it a reasonably clean bill of health.
49 * The modulus %$p$% is chosen as the largest prime number less than
50 * %$2^{32}$%. The multiplier %$a$% and additive constant %$c$% are based on
51 * the decimal expansions of %$\pi$% and %$e$%, with the additional
52 * restriction that the multiplier must be a primitive element modulo %$p$%.
53 * The fixed point value is determined as %$c / (1 - a) \bmod p$%.
56 #ifndef CATACOMB_LCRAND_H
57 #define CATACOMB_LCRAND_H
63 /*----- Header files ------------------------------------------------------*/
65 #include <mLib/bits.h>
67 #ifndef CATACOMB_GRAND_H
71 /*----- Constants ---------------------------------------------------------*/
73 #define LCRAND_P 4294967291u /* Modulus for the generator */
74 #define LCRAND_A 314159265u /* Multiplier (primitive mod @p@) */
75 #define LCRAND_C 271828183u /* Additive constant */
77 #define LCRAND_FIXEDPT 3223959250u /* Fixed point (only bad input) */
79 /*----- Functions provided ------------------------------------------------*/
83 * Arguments: @uint32 x@ = seed value
85 * Returns: New state of the generator.
87 * Use: Steps the generator. Returns %$ax + c \bmod p$%.
90 extern uint32 lcrand(uint32 /*x*/);
92 /* --- @lcrand_range@ --- *
94 * Arguments: @uint32 *x@ = pointer to seed value (updated)
95 * @uint32 m@ = limit allowable
97 * Returns: A uniformly distributed pseudorandom integer in the interval
101 extern uint32 lcrand_range(uint32 */*x*/, uint32 /*m*/);
103 /* --- @lcrand_create@ --- *
105 * Arguments: @uint32 x@ = initial seed
107 * Returns: Pointer to a generic generator.
109 * Use: Constructs a generic generator interface over a linear
110 * congruential generator.
113 extern grand *lcrand_create(uint32 /*x*/);
115 /*----- That's all, folks -------------------------------------------------*/