| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: lcrand.h,v 1.1 1999/12/10 23:15:27 mdw Exp $ |
| 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 $ |
| 33 | * Revision 1.1 1999/12/10 23:15:27 mdw |
| 34 | * Noncryptographic random number generator. |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | #ifndef CATACOMB_LCRAND_H |
| 39 | #define CATACOMB_LCRAND_H |
| 40 | |
| 41 | #ifdef __cplusplus |
| 42 | extern "C" { |
| 43 | #endif |
| 44 | |
| 45 | /*----- Notes on the linear congruential generator ------------------------* |
| 46 | * |
| 47 | * This pseudorandom number generator is simple, but has absolutely no |
| 48 | * cryptographic strength whatever. It may be used whenever random numbers |
| 49 | * are required but cryptographic strength is not, for example when |
| 50 | * generating numbers for use in primality tests. To be honest, it's not |
| 51 | * even particularly fast, although a certain amount of effort has been |
| 52 | * expended on making it better than awfully slow. |
| 53 | * |
| 54 | * There exists a fixed-point input @LCRAND_FIXEDPT@ -- when fed to the |
| 55 | * generator it comes straight back out again. All other inputs less than |
| 56 | * the modulus are part of the same sequence of period %$p - 1$%. |
| 57 | * |
| 58 | * The generator has been tested for its statistical properties. George |
| 59 | * Marsaglia's Diehard tests give it a reasonably clean bill of health. |
| 60 | * |
| 61 | * The modulus %$p$% is chosen as the largest prime number less than |
| 62 | * %$2^{32}$%. The multiplier %$a$% and additive constant %$c$% are based on |
| 63 | * the decimal expansions of %$\pi$% and %$e$%, with the additional |
| 64 | * restriction that the multiplier must be a primitive element modulo %$p$%. |
| 65 | * The fixed point value is determined as %$c / (1 - a) \bmod p$%. |
| 66 | */ |
| 67 | |
| 68 | /*----- Header files ------------------------------------------------------*/ |
| 69 | |
| 70 | #include <mLib/bits.h> |
| 71 | |
| 72 | #ifndef CATACOMB_GRAND_H |
| 73 | # include "grand.h" |
| 74 | #endif |
| 75 | |
| 76 | /*----- Constants ---------------------------------------------------------*/ |
| 77 | |
| 78 | #define LCRAND_P 4294967291u /* Modulus for the generator */ |
| 79 | #define LCRAND_A 314159265u /* Multiplier (primitive mod @p@) */ |
| 80 | #define LCRAND_C 271828183u /* Additive constant */ |
| 81 | |
| 82 | #define LCRAND_FIXEDPT 3223959250u /* Fixed point (only bad input) */ |
| 83 | |
| 84 | /*----- Functions provided ------------------------------------------------*/ |
| 85 | |
| 86 | /* --- @lcrand@ --- * |
| 87 | * |
| 88 | * Arguments: @uint32 x@ = seed value |
| 89 | * |
| 90 | * Returns: New state of the generator. |
| 91 | * |
| 92 | * Use: Steps the generator. Returns %$ax + c \bmod p$%. |
| 93 | */ |
| 94 | |
| 95 | extern uint32 lcrand(uint32 /*x*/); |
| 96 | |
| 97 | /* --- @lcrand_range@ --- * |
| 98 | * |
| 99 | * Arguments: @uint32 *x@ = pointer to seed value (updated) |
| 100 | * @uint32 m@ = limit allowable |
| 101 | * |
| 102 | * Returns: A uniformly distributed pseudorandom integer in the interval |
| 103 | * %$[0, m)$%. |
| 104 | */ |
| 105 | |
| 106 | extern uint32 lcrand_range(uint32 */*x*/, uint32 /*m*/); |
| 107 | |
| 108 | /* --- @lcrand_create@ --- * |
| 109 | * |
| 110 | * Arguments: @uint32 x@ = initial seed |
| 111 | * |
| 112 | * Returns: Pointer to a generic generator. |
| 113 | * |
| 114 | * Use: Constructs a generic generator interface over a linear |
| 115 | * congruential generator. |
| 116 | */ |
| 117 | |
| 118 | extern grand *lcrand_create(uint32 /*x*/); |
| 119 | |
| 120 | /*----- That's all, folks -------------------------------------------------*/ |
| 121 | |
| 122 | #ifdef __cplusplus |
| 123 | } |
| 124 | #endif |
| 125 | |
| 126 | #endif |