chiark / gitweb /
Version bump.
[storin] / lcrand.h
1 /* -*-c-*-
2  *
3  * $Id: lcrand.h,v 1.2 2000/07/02 15:21:20 mdw Exp $
4  *
5  * Simple linear congruential generator
6  *
7  * (c) 1999 Straylight/Edgeware
8  * (c) 2000 Mark Wooding
9  */
10
11 /*----- Licensing notice --------------------------------------------------* 
12  *
13  * Copyright (c) 2000 Mark Wooding
14  * All rights reserved.
15  * 
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions are
18  * met:
19  * 
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 
23  * 2, Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 
27  * 3. The name of the authors may not be used to endorse or promote
28  *    products derived from this software without specific prior written
29  *    permission.
30  * 
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
34  * NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  * 
43  * Instead of accepting the above terms, you may redistribute and/or modify
44  * this software under the terms of either the GNU General Public License,
45  * or the GNU Library General Public License, published by the Free
46  * Software Foundation; either version 2 of the License, or (at your
47  * option) any later version.
48  */
49
50 /*----- Revision history --------------------------------------------------* 
51  *
52  * $Log: lcrand.h,v $
53  * Revision 1.2  2000/07/02 15:21:20  mdw
54  * Fix licence text.
55  *
56  * Revision 1.1  2000/05/21 11:28:30  mdw
57  * Initial check-in.
58  *
59  * --- Past lives (Catacomb) --- *
60  *
61  * Revision 1.1  1999/12/10 23:15:27  mdw
62  * Noncryptographic random number generator.
63  *
64  */
65
66 /*----- Notes on the linear congruential generator ------------------------*
67  *
68  * This pseudorandom number generator is simple, but has absolutely no
69  * cryptographic strength whatever.  It may be used whenever random numbers
70  * are required but cryptographic strength is not, for example when
71  * generating numbers for use in primality tests.  To be honest, it's not
72  * even particularly fast, although a certain amount of effort has been
73  * expended on making it better than awfully slow.  To put things in
74  * perspective, it can't quite spit bytes out as fast as OFB DES.  (Then
75  * again, bytes aren't its natural output format.)  Its main use is probably
76  * seeding a Fibonacci generator.
77  *
78  * There exists a fixed-point input @LCRAND_FIXEDPT@ -- when fed to the
79  * generator it comes straight back out again.  All other inputs less than
80  * the modulus are part of the same sequence of period %$p - 1$%.
81  *
82  * The generator has been tested for its statistical properties.  George
83  * Marsaglia's Diehard tests give it a reasonably clean bill of health.
84  *
85  * The modulus %$p$% is chosen as the largest prime number less than
86  * %$2^{32}$%.  The multiplier %$a$% and additive constant %$c$% are based on
87  * the decimal expansions of %$\pi$% and %$e$%, with the additional
88  * restriction that the multiplier must be a primitive element modulo %$p$%.
89  * The fixed point value is determined as %$c / (1 - a) \bmod p$%.
90  */
91
92 #ifndef LCRAND_H
93 #define LCRAND_H
94
95 #ifdef __cplusplus
96   extern "C" {
97 #endif
98
99 /*----- Header files ------------------------------------------------------*/
100
101 #ifndef BITS_H
102 #  include "bits.h"
103 #endif
104
105 /*----- Constants ---------------------------------------------------------*/
106
107 #define LCRAND_P 4294967291u            /* Modulus for the generator */
108 #define LCRAND_A 314159265u             /* Multiplier (primitive mod @p@) */
109 #define LCRAND_C 271828183u             /* Additive constant */
110
111 #define LCRAND_FIXEDPT 3223959250u      /* Fixed point (only bad input) */
112
113 /*----- Functions provided ------------------------------------------------*/
114
115 /* --- @lcrand@ --- *
116  *
117  * Arguments:   @uint32 x@ = seed value
118  *
119  * Returns:     New state of the generator.
120  *
121  * Use:         Steps the generator.  Returns %$ax + c \bmod p$%.
122  */
123
124 extern uint32 lcrand(uint32 /*x*/);
125
126 /* --- @lcrand_range@ --- *
127  *
128  * Arguments:   @uint32 *x@ = pointer to seed value (updated)
129  *              @uint32 m@ = limit allowable
130  *
131  * Returns:     A uniformly distributed pseudorandom integer in the interval
132  *              %$[0, m)$%.
133  */
134
135 extern uint32 lcrand_range(uint32 */*x*/, uint32 /*m*/);
136
137 /*----- That's all, folks -------------------------------------------------*/
138
139 #ifdef __cplusplus
140   }
141 #endif
142
143 #endif