3 * $Id: grand.c,v 1.1 1999/12/10 23:16:01 mdw Exp $
5 * Generic interface to random number generators
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 1999/12/10 23:16:01 mdw
38 /*----- Header files ------------------------------------------------------*/
42 #include <mLib/bits.h>
46 /*----- Main code ---------------------------------------------------------*/
48 /* --- @grand_byte@ --- *
50 * Arguments: @grand *r@ = pointet to generic generator
52 * Returns: A uniformly-distributed pseudorandom integer in the interval
56 octet grand_byte(grand *r)
58 if (r->ops->byte != grand_byte)
59 return (r->ops->byte(r));
60 else if (r->ops->word != grand_word)
61 return (r->ops->word(r) & 0xff);
62 else if (r->ops->fill != grand_fill) {
64 r->ops->fill(r, &o, 1);
67 return (grand_range(r, 256));
70 /* --- @grand_word@ --- *
72 * Arguments: @grand *r@ = pointet to generic generator
74 * Returns: A uniformly-distributed pseudorandom integer in the interval
78 uint32 grand_word(grand *r)
80 if (r->ops->word != grand_word)
81 return (r->ops->word(r));
84 grand_fill(r, b, sizeof(b));
89 /* --- @grand_range@ --- *
91 * Arguments: @grand *r@ = pointet to generic generator
92 * @uint32 l@ = limit for acceptable results
94 * Returns: A uniformly-distributed pseudorandom integer in the interval
98 uint32 grand_range(grand *r, uint32 l)
100 if (r->ops->range != grand_range)
101 return (r->ops->range(r, l));
104 uint32 (*w)(grand */*r*/);
107 /* --- Decide where to get data from --- *
109 * The choice of %$2^{32} - 1$% as a limit when using @grand_word@ isn't
110 * wonderful, but working with %$2^{32}$% is awkward and the loss of a
111 * few return values isn't significant. The algorithm below still
112 * successfully returns uniformly distributed results.
123 /* --- Work out maximum acceptable return value --- *
125 * This will be the highest multiple of @l@ less than @m@.
131 /* --- Generate numbers until something acceptable is found --- *
133 * This will require an expected number of attempts less than 2.
136 do x = w(r); while (x >= z);
141 /* --- @grand_fill@ --- *
143 * Arguments: @grand *r@ = pointet to generic generator
144 * @void *p@ = pointer to a buffer
145 * @size_t sz@ = size of the buffer
149 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
150 * (see @grand_byte@).
153 void grand_fill(grand *r, void *p, size_t sz)
155 if (r->ops->fill != grand_fill)
156 r->ops->fill(r, p, sz);
160 *q++ = r->ops->byte(r);
166 /*----- That's all, folks -------------------------------------------------*/