3 * $Id: grand.c,v 1.3 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/bits.h>
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @grand_byte@ --- *
42 * Arguments: @grand *r@ = pointet to generic generator
44 * Returns: A uniformly-distributed pseudorandom integer in the interval
48 octet grand_byte(grand *r)
50 if (r->ops->byte != grand_byte)
51 return (r->ops->byte(r));
52 else if (r->ops->word != grand_word)
53 return (r->ops->word(r) & 0xff);
54 else if (r->ops->fill != grand_fill) {
56 r->ops->fill(r, &o, 1);
59 return (grand_range(r, 256));
62 /* --- @grand_word@ --- *
64 * Arguments: @grand *r@ = pointet to generic generator
66 * Returns: A uniformly-distributed pseudorandom integer in the interval
70 uint32 grand_word(grand *r)
72 if (r->ops->word != grand_word)
73 return (r->ops->word(r));
76 grand_fill(r, b, sizeof(b));
81 /* --- @grand_range@ --- *
83 * Arguments: @grand *r@ = pointet to generic generator
84 * @uint32 l@ = limit for acceptable results
86 * Returns: A uniformly-distributed pseudorandom integer in the interval
90 uint32 grand_range(grand *r, uint32 l)
92 if (r->ops->range != grand_range)
93 return (r->ops->range(r, l));
96 uint32 (*w)(grand */*r*/);
99 /* --- Decide where to get data from --- *
101 * The choice of %$2^{32} - 1$% as a limit when using @grand_word@ isn't
102 * wonderful, but working with %$2^{32}$% is awkward and the loss of a
103 * few return values isn't significant. The algorithm below still
104 * successfully returns uniformly distributed results.
115 /* --- Work out maximum acceptable return value --- *
117 * This will be the highest multiple of @l@ less than @m@.
122 /* --- Generate numbers until something acceptable is found --- *
124 * This will require an expected number of attempts less than 2.
127 do x = w(r); while (x >= z);
132 /* --- @grand_fill@ --- *
134 * Arguments: @grand *r@ = pointet to generic generator
135 * @void *p@ = pointer to a buffer
136 * @size_t sz@ = size of the buffer
140 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
141 * (see @grand_byte@).
144 void grand_fill(grand *r, void *p, size_t sz)
146 if (r->ops->fill != grand_fill)
147 r->ops->fill(r, p, sz);
151 *q++ = r->ops->byte(r);
157 /*----- That's all, folks -------------------------------------------------*/