3 * $Id: grand.c,v 1.2 2000/12/06 20:31:06 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.2 2000/12/06 20:31:06 mdw
34 * Simplify uniform range transformation.
36 * Revision 1.1 1999/12/10 23:16:01 mdw
41 /*----- Header files ------------------------------------------------------*/
45 #include <mLib/bits.h>
49 /*----- Main code ---------------------------------------------------------*/
51 /* --- @grand_byte@ --- *
53 * Arguments: @grand *r@ = pointet to generic generator
55 * Returns: A uniformly-distributed pseudorandom integer in the interval
59 octet
grand_byte(grand
*r
)
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
) {
67 r
->ops
->fill(r
, &o
, 1);
70 return (grand_range(r
, 256));
73 /* --- @grand_word@ --- *
75 * Arguments: @grand *r@ = pointet to generic generator
77 * Returns: A uniformly-distributed pseudorandom integer in the interval
81 uint32
grand_word(grand
*r
)
83 if (r
->ops
->word
!= grand_word
)
84 return (r
->ops
->word(r
));
87 grand_fill(r
, b
, sizeof(b
));
92 /* --- @grand_range@ --- *
94 * Arguments: @grand *r@ = pointet to generic generator
95 * @uint32 l@ = limit for acceptable results
97 * Returns: A uniformly-distributed pseudorandom integer in the interval
101 uint32
grand_range(grand
*r
, uint32 l
)
103 if (r
->ops
->range
!= grand_range
)
104 return (r
->ops
->range(r
, l
));
107 uint32 (*w
)(grand */
*r*/
);
110 /* --- Decide where to get data from --- *
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.
126 /* --- Work out maximum acceptable return value --- *
128 * This will be the highest multiple of @l@ less than @m@.
133 /* --- Generate numbers until something acceptable is found --- *
135 * This will require an expected number of attempts less than 2.
138 do x
= w(r
); while (x
>= z
);
143 /* --- @grand_fill@ --- *
145 * Arguments: @grand *r@ = pointet to generic generator
146 * @void *p@ = pointer to a buffer
147 * @size_t sz@ = size of the buffer
151 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
152 * (see @grand_byte@).
155 void grand_fill(grand
*r
, void *p
, size_t sz
)
157 if (r
->ops
->fill
!= grand_fill
)
158 r
->ops
->fill(r
, p
, sz
);
162 *q
++ = r
->ops
->byte(r
);
168 /*----- That's all, folks -------------------------------------------------*/