More changes. Still embryonic.
[u/mdw/catacomb] / grand.c
CommitLineData
aa1082f2 1/* -*-c-*-
2 *
3 * $Id: grand.c,v 1.1 1999/12/10 23:16:01 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.1 1999/12/10 23:16:01 mdw
34 * Generic interface.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <stddef.h>
41
42#include <mLib/bits.h>
43
44#include "grand.h"
45
46/*----- Main code ---------------------------------------------------------*/
47
48/* --- @grand_byte@ --- *
49 *
50 * Arguments: @grand *r@ = pointet to generic generator
51 *
52 * Returns: A uniformly-distributed pseudorandom integer in the interval
53 * %$[0, 256)$%.
54 */
55
56octet grand_byte(grand *r)
57{
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) {
63 octet o;
64 r->ops->fill(r, &o, 1);
65 return (o);
66 } else
67 return (grand_range(r, 256));
68}
69
70/* --- @grand_word@ --- *
71 *
72 * Arguments: @grand *r@ = pointet to generic generator
73 *
74 * Returns: A uniformly-distributed pseudorandom integer in the interval
75 * %$[0, 2^{32})$%.
76 */
77
78uint32 grand_word(grand *r)
79{
80 if (r->ops->word != grand_word)
81 return (r->ops->word(r));
82 else {
83 octet b[4];
84 grand_fill(r, b, sizeof(b));
85 return (LOAD32(b));
86 }
87}
88
89/* --- @grand_range@ --- *
90 *
91 * Arguments: @grand *r@ = pointet to generic generator
92 * @uint32 l@ = limit for acceptable results
93 *
94 * Returns: A uniformly-distributed pseudorandom integer in the interval
95 * %$[0, l)$%.
96 */
97
98uint32 grand_range(grand *r, uint32 l)
99{
100 if (r->ops->range != grand_range)
101 return (r->ops->range(r, l));
102 else {
103 uint32 m, z;
104 uint32 (*w)(grand */*r*/);
105 uint32 x;
106
107 /* --- Decide where to get data from --- *
108 *
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.
113 */
114
115 if (r->ops->max) {
116 w = r->ops->raw;
117 m = r->ops->max;
118 } else {
119 w = grand_word;
120 m = 0xffffffff;
121 }
122
123 /* --- Work out maximum acceptable return value --- *
124 *
125 * This will be the highest multiple of @l@ less than @m@.
126 */
127
128 z = m - (m % l);
129 m = z / l;
130
131 /* --- Generate numbers until something acceptable is found --- *
132 *
133 * This will require an expected number of attempts less than 2.
134 */
135
136 do x = w(r); while (x >= z);
137 return (x / m);
138 }
139}
140
141/* --- @grand_fill@ --- *
142 *
143 * Arguments: @grand *r@ = pointet to generic generator
144 * @void *p@ = pointer to a buffer
145 * @size_t sz@ = size of the buffer
146 *
147 * Returns: ---
148 *
149 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
150 * (see @grand_byte@).
151 */
152
153void grand_fill(grand *r, void *p, size_t sz)
154{
155 if (r->ops->fill != grand_fill)
156 r->ops->fill(r, p, sz);
157 else {
158 octet *q = p;
159 while (sz) {
160 *q++ = r->ops->byte(r);
161 sz--;
162 }
163 }
164}
165
166/*----- That's all, folks -------------------------------------------------*/