5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Notes on the Fibonacci generator ----------------------------------*
30 * The generator was originally suggested by G. J. Mitchell and D. P. Moore
31 * in 1957, and publicized by D. E. Knuth as Algorithm 3.2.2A in volume 2 of
32 * his work `The Art of Computer Programming'. The generator is simple: at
33 * each stage it emits %$x_n = (x_{n - 55} + x_{n - 24}) \bmod 2^{32}$%. The
34 * period is proven to be greater than %$2^{55}$%, and statistical properties
38 #ifndef CATACOMB_FIBRAND_H
39 #define CATACOMB_FIBRAND_H
45 /*----- Header files ------------------------------------------------------*/
47 #include <mLib/bits.h>
49 #ifndef CATACOMB_GRAND_H
53 /*----- Magic constants ---------------------------------------------------*/
58 /*----- Data structures ---------------------------------------------------*/
60 typedef struct fibrand {
65 /*----- Functions provided ------------------------------------------------*/
67 /* --- @fibrand_step@ --- *
69 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
71 * Returns: Next output from generator.
73 * Use: Steps the generator. Returns
74 * %$x_{i - 24} + x_{i - 55} \bmod 2^{32}$%.
77 extern uint32 fibrand_step(fibrand */*f*/);
79 /* --- @fibrand_seed@ --- *
81 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
82 * @grand *r@ = random number generator to extract words from
86 * Use: Initializes a Fibonacci generator using word outputs from the
87 * given random number source @r@.
90 extern void fibrand_seed(fibrand */*f*/, grand */*r*/);
92 /* --- @fibrand_lcseed@ --- *
94 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
95 * @uint32 seed@ = seed value
99 * Use: Initializes a Fibonacci generator using outputs from the
100 * @lcrand@ generator seeded from @seed@. This is faster than
101 * using a generic @lcrand@-based generator and @fibrand_rseed@
102 * because it uses raw outputs rather than uniformly distributed
106 extern void fibrand_lcseed(fibrand */*f*/, uint32 /*seed*/);
108 /* --- @fibrand_range@ --- *
110 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
113 * Returns: A uniformly distributed pseudorandom integer in the interval
117 extern uint32 fibrand_range(fibrand */*f*/, uint32 /*m*/);
119 /* --- @fibrand_create@ --- *
121 * Arguments: @uint32 seed@ = initial seed
123 * Returns: Pointer to a generic generator.
125 * Use: Constructs a generic generator interface over a Fibonacci
126 * generator. The generator is seeded using @fibrand_lcseed@.
129 extern grand *fibrand_create(uint32 /*seed*/);
131 /*----- That's all, folks -------------------------------------------------*/