b7118ab21eb1450da3a6554f2b5cb99e492fbd34
[u/mdw/catacomb] / grand.c
1 /* -*-c-*-
2 *
3 * $Id: grand.c,v 1.2 2000/12/06 20:31:06 mdw Exp $
4 *
5 * Generic interface to random number generators
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: grand.c,v $
33 * Revision 1.2 2000/12/06 20:31:06 mdw
34 * Simplify uniform range transformation.
35 *
36 * Revision 1.1 1999/12/10 23:16:01 mdw
37 * Generic interface.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <stddef.h>
44
45 #include <mLib/bits.h>
46
47 #include "grand.h"
48
49 /*----- Main code ---------------------------------------------------------*/
50
51 /* --- @grand_byte@ --- *
52 *
53 * Arguments: @grand *r@ = pointet to generic generator
54 *
55 * Returns: A uniformly-distributed pseudorandom integer in the interval
56 * %$[0, 256)$%.
57 */
58
59 octet grand_byte(grand *r)
60 {
61 if (r->ops->byte != grand_byte)
62 return (r->ops->byte(r));
63 else if (r->ops->word != grand_word)
64 return (r->ops->word(r) & 0xff);
65 else if (r->ops->fill != grand_fill) {
66 octet o;
67 r->ops->fill(r, &o, 1);
68 return (o);
69 } else
70 return (grand_range(r, 256));
71 }
72
73 /* --- @grand_word@ --- *
74 *
75 * Arguments: @grand *r@ = pointet to generic generator
76 *
77 * Returns: A uniformly-distributed pseudorandom integer in the interval
78 * %$[0, 2^{32})$%.
79 */
80
81 uint32 grand_word(grand *r)
82 {
83 if (r->ops->word != grand_word)
84 return (r->ops->word(r));
85 else {
86 octet b[4];
87 grand_fill(r, b, sizeof(b));
88 return (LOAD32(b));
89 }
90 }
91
92 /* --- @grand_range@ --- *
93 *
94 * Arguments: @grand *r@ = pointet to generic generator
95 * @uint32 l@ = limit for acceptable results
96 *
97 * Returns: A uniformly-distributed pseudorandom integer in the interval
98 * %$[0, l)$%.
99 */
100
101 uint32 grand_range(grand *r, uint32 l)
102 {
103 if (r->ops->range != grand_range)
104 return (r->ops->range(r, l));
105 else {
106 uint32 m, z;
107 uint32 (*w)(grand */*r*/);
108 uint32 x;
109
110 /* --- Decide where to get data from --- *
111 *
112 * The choice of %$2^{32} - 1$% as a limit when using @grand_word@ isn't
113 * wonderful, but working with %$2^{32}$% is awkward and the loss of a
114 * few return values isn't significant. The algorithm below still
115 * successfully returns uniformly distributed results.
116 */
117
118 if (r->ops->max) {
119 w = r->ops->raw;
120 m = r->ops->max;
121 } else {
122 w = grand_word;
123 m = 0xffffffff;
124 }
125
126 /* --- Work out maximum acceptable return value --- *
127 *
128 * This will be the highest multiple of @l@ less than @m@.
129 */
130
131 z = m - (m % l);
132
133 /* --- Generate numbers until something acceptable is found --- *
134 *
135 * This will require an expected number of attempts less than 2.
136 */
137
138 do x = w(r); while (x >= z);
139 return (x % l);
140 }
141 }
142
143 /* --- @grand_fill@ --- *
144 *
145 * Arguments: @grand *r@ = pointet to generic generator
146 * @void *p@ = pointer to a buffer
147 * @size_t sz@ = size of the buffer
148 *
149 * Returns: ---
150 *
151 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
152 * (see @grand_byte@).
153 */
154
155 void grand_fill(grand *r, void *p, size_t sz)
156 {
157 if (r->ops->fill != grand_fill)
158 r->ops->fill(r, p, sz);
159 else {
160 octet *q = p;
161 while (sz) {
162 *q++ = r->ops->byte(r);
163 sz--;
164 }
165 }
166 }
167
168 /*----- That's all, folks -------------------------------------------------*/