| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: fibrand.c,v 1.2 2000/07/02 15:21:20 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 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: fibrand.c,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 | /*----- Header files ------------------------------------------------------*/ |
| 67 | |
| 68 | #include <stdio.h> |
| 69 | #include <stdlib.h> |
| 70 | #include <string.h> |
| 71 | |
| 72 | #include "bits.h" |
| 73 | #include "fibrand.h" |
| 74 | #include "lcrand.h" |
| 75 | |
| 76 | /*----- Main code ---------------------------------------------------------*/ |
| 77 | |
| 78 | /* --- @fibrand_step@ --- * |
| 79 | * |
| 80 | * Arguments: @fibrand *f@ = pointer to Fibonacci generator context |
| 81 | * |
| 82 | * Returns: Next output from generator. |
| 83 | * |
| 84 | * Use: Steps the generator. Returns |
| 85 | * %$x_{i - 24} + x_{i - 55} \bmod 2^{32}$%. |
| 86 | */ |
| 87 | |
| 88 | uint32 fibrand_step(fibrand *f) |
| 89 | { |
| 90 | unsigned i = f->i; |
| 91 | unsigned j = i + (FIB_SZ - FIB_TAP); |
| 92 | uint32 x; |
| 93 | if (j >= FIB_SZ) |
| 94 | j -= FIB_SZ; |
| 95 | x = f->x[i] = U32(f->x[i] + f->x[j]); |
| 96 | i++; |
| 97 | if (i >= FIB_SZ) |
| 98 | i = 0; |
| 99 | f->i = i; |
| 100 | return (x); |
| 101 | } |
| 102 | |
| 103 | /* --- @fibrand_lcseed@ --- * |
| 104 | * |
| 105 | * Arguments: @fibrand *f@ = pointer to Fibonacci generator context |
| 106 | * @uint32 seed@ = seed value |
| 107 | * |
| 108 | * Returns: --- |
| 109 | * |
| 110 | * Use: Initializes a Fibonacci generator using outputs from the |
| 111 | * @lcrand@ generator seeded from @seed@. This is faster than |
| 112 | * using a generic @lcrand@-based generator and @fibrand_rseed@ |
| 113 | * because it uses raw outputs rather than uniformly distributed |
| 114 | * 32-bit words. |
| 115 | */ |
| 116 | |
| 117 | void fibrand_lcseed(fibrand *f, uint32 seed) |
| 118 | { |
| 119 | int i; |
| 120 | unsigned p = 0; |
| 121 | |
| 122 | for (i = 0; i < FIB_SZ; i++) |
| 123 | p |= f->x[i] = seed = lcrand(seed); |
| 124 | if (!(p & 1)) { |
| 125 | i = lcrand_range(&seed, FIB_SZ); |
| 126 | f->x[i] |= 1; |
| 127 | } |
| 128 | f->i = 0; |
| 129 | } |
| 130 | |
| 131 | /* --- @fibrand_range@ --- * |
| 132 | * |
| 133 | * Arguments: @fibrand *f@ = pointer to Fibonacci generator context |
| 134 | * @uint32 m@ = limit |
| 135 | * |
| 136 | * Returns: A uniformly distributed pseudorandom integer in the interval |
| 137 | * %$[0, m)$%. |
| 138 | */ |
| 139 | |
| 140 | uint32 fibrand_range(fibrand *f, uint32 m) |
| 141 | { |
| 142 | uint32 r = 0xffffffff - (0xffffffff % m); |
| 143 | uint x; |
| 144 | |
| 145 | /* --- Now generate numbers until a good one comes along --- */ |
| 146 | |
| 147 | do x = fibrand_step(f); while (x >= r); |
| 148 | return (x / (r / m)); |
| 149 | } |
| 150 | |
| 151 | /*----- That's all, folks -------------------------------------------------*/ |