chiark / gitweb /
Improve analysis section.
[storin] / fibrand.h
1 /* -*-c-*-
2  *
3  * $Id: fibrand.h,v 1.1 2000/05/21 11:28:30 mdw Exp $
4  *
5  * Fibonacci 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 REGENTS 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: fibrand.h,v $
53  * Revision 1.1  2000/05/21 11:28:30  mdw
54  * Initial check-in.
55  *
56  * --- Past lives (Catacomb) --- *
57  *
58  * Revision 1.1  1999/12/10 23:15:27  mdw
59  * Noncryptographic random number generator.
60  *
61  */
62
63 /*----- Notes on the Fibonacci generator ----------------------------------*
64  *
65  * The generator was originally suggested by G. J. Mitchell and D. P. Moore
66  * in 1957, and publicized by D. E. Knuth as Algorithm 3.2.2A in volume 2 of
67  * his work `The Art of Computer Programming'.  The generator is simple: at
68  * each stage it emits %$x_n = (x_{n - 55} + x_{n - 24}) \bmod 2^{32}$%.  The
69  * period is proven to be greater than %$2^{55}$%, and statistical properties
70  * appear to be good.
71  */
72
73 #ifndef FIBRAND_H
74 #define FIBRAND_H
75
76 #ifdef __cplusplus
77   extern "C" {
78 #endif
79
80 /*----- Header files ------------------------------------------------------*/
81
82 #ifndef BITS_H
83 #  include "bits.h"
84 #endif
85
86 /*----- Magic constants ---------------------------------------------------*/
87
88 #define FIB_SZ 55
89 #define FIB_TAP 24
90
91 /*----- Data structures ---------------------------------------------------*/
92
93 typedef struct fibrand {
94   unsigned i;
95   uint32 x[FIB_SZ];
96 } fibrand;
97
98 /*----- Functions provided ------------------------------------------------*/
99
100 /* --- @fibrand_step@ --- *
101  *
102  * Arguments:   @fibrand *f@ = pointer to Fibonacci generator context
103  *
104  * Returns:     Next output from generator.
105  *
106  * Use:         Steps the generator.  Returns
107  *              %$x_{i - 24} + x_{i - 55} \bmod 2^{32}$%.
108  */
109
110 extern uint32 fibrand_step(fibrand */*f*/);
111
112 /* --- @fibrand_lcseed@ --- *
113  *
114  * Arguments:   @fibrand *f@ = pointer to Fibonacci generator context
115  *              @uint32 seed@ = seed value
116  *
117  * Returns:     ---
118  *
119  * Use:         Initializes a Fibonacci generator using outputs from the
120  *              @lcrand@ generator seeded from @seed@.  This is faster than
121  *              using a generic @lcrand@-based generator and @fibrand_rseed@
122  *              because it uses raw outputs rather than uniformly distributed
123  *              32-bit words.
124  */
125
126 extern void fibrand_lcseed(fibrand */*f*/, uint32 /*seed*/);
127
128 /* --- @fibrand_range@ --- *
129  *
130  * Arguments:   @fibrand *f@ = pointer to Fibonacci generator context
131  *              @uint32 m@ = limit
132  *
133  * Returns:     A uniformly distributed pseudorandom integer in the interval
134  *              %$[0, m)$%.
135  */
136
137 extern uint32 fibrand_range(fibrand */*f*/, uint32 /*m*/);
138
139 /*----- That's all, folks -------------------------------------------------*/
140
141 #ifdef __cplusplus
142   }
143 #endif
144
145 #endif