3 * $Id: fibrand.h,v 1.3 2004/04/08 01:36:15 mdw Exp $
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 Fibonacci generator ----------------------------------*
32 * The generator was originally suggested by G. J. Mitchell and D. P. Moore
33 * in 1957, and publicized by D. E. Knuth as Algorithm 3.2.2A in volume 2 of
34 * his work `The Art of Computer Programming'. The generator is simple: at
35 * each stage it emits %$x_n = (x_{n - 55} + x_{n - 24}) \bmod 2^{32}$%. The
36 * period is proven to be greater than %$2^{55}$%, and statistical properties
40 #ifndef CATACOMB_FIBRAND_H
41 #define CATACOMB_FIBRAND_H
47 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/bits.h>
51 #ifndef CATACOMB_GRAND_H
55 /*----- Magic constants ---------------------------------------------------*/
60 /*----- Data structures ---------------------------------------------------*/
62 typedef struct fibrand
{
67 /*----- Functions provided ------------------------------------------------*/
69 /* --- @fibrand_step@ --- *
71 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
73 * Returns: Next output from generator.
75 * Use: Steps the generator. Returns
76 * %$x_{i - 24} + x_{i - 55} \bmod 2^{32}$%.
79 extern uint32
fibrand_step(fibrand */
*f*/
);
81 /* --- @fibrand_seed@ --- *
83 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
84 * @grand *r@ = random number generator to extract words from
88 * Use: Initializes a Fibonacci generator using word outputs from the
89 * given random number source @r@.
92 extern void fibrand_seed(fibrand */
*f*/
, grand */
*r*/
);
94 /* --- @fibrand_lcseed@ --- *
96 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
97 * @uint32 seed@ = seed value
101 * Use: Initializes a Fibonacci generator using outputs from the
102 * @lcrand@ generator seeded from @seed@. This is faster than
103 * using a generic @lcrand@-based generator and @fibrand_rseed@
104 * because it uses raw outputs rather than uniformly distributed
108 extern void fibrand_lcseed(fibrand */
*f*/
, uint32
/*seed*/);
110 /* --- @fibrand_range@ --- *
112 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
115 * Returns: A uniformly distributed pseudorandom integer in the interval
119 extern uint32
fibrand_range(fibrand */
*f*/
, uint32
/*m*/);
121 /* --- @fibrand_create@ --- *
123 * Arguments: @uint32 seed@ = initial seed
125 * Returns: Pointer to a generic generator.
127 * Use: Constructs a generic generator interface over a Fibonacci
128 * generator. The generator is seeded using @fibrand_lcseed@.
131 extern grand
*fibrand_create(uint32
/*seed*/);
133 /*----- That's all, folks -------------------------------------------------*/