3 * Generic interface to random number generators
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/bits.h>
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @grand_byte@ --- *
40 * Arguments: @grand *r@ = pointet to generic generator
42 * Returns: A uniformly-distributed pseudorandom integer in the interval
46 octet
grand_byte(grand
*r
)
48 if (r
->ops
->byte
!= grand_byte
)
49 return (r
->ops
->byte(r
));
50 else if (r
->ops
->word
!= grand_word
)
51 return (r
->ops
->word(r
) & 0xff);
52 else if (r
->ops
->fill
!= grand_fill
) {
54 r
->ops
->fill(r
, &o
, 1);
57 return (grand_range(r
, 256));
60 /* --- @grand_word@ --- *
62 * Arguments: @grand *r@ = pointet to generic generator
64 * Returns: A uniformly-distributed pseudorandom integer in the interval
68 uint32
grand_word(grand
*r
)
70 if (r
->ops
->word
!= grand_word
)
71 return (r
->ops
->word(r
));
74 grand_fill(r
, b
, sizeof(b
));
79 /* --- @grand_range@ --- *
81 * Arguments: @grand *r@ = pointet to generic generator
82 * @uint32 l@ = limit for acceptable results
84 * Returns: A uniformly-distributed pseudorandom integer in the interval
88 uint32
grand_range(grand
*r
, uint32 l
)
90 if (r
->ops
->range
!= grand_range
)
91 return (r
->ops
->range(r
, l
));
94 uint32 (*w
)(grand */
*r*/
);
97 /* --- Decide where to get data from --- *
99 * The choice of %$2^{32} - 1$% as a limit when using @grand_word@ isn't
100 * wonderful, but working with %$2^{32}$% is awkward and the loss of a
101 * few return values isn't significant. The algorithm below still
102 * successfully returns uniformly distributed results.
113 /* --- Work out maximum acceptable return value --- *
115 * This will be the highest multiple of @l@ less than @m@.
120 /* --- Generate numbers until something acceptable is found --- *
122 * This will require an expected number of attempts less than 2.
125 do x
= w(r
); while (x
>= z
);
130 /* --- @grand_fill@ --- *
132 * Arguments: @grand *r@ = pointet to generic generator
133 * @void *p@ = pointer to a buffer
134 * @size_t sz@ = size of the buffer
138 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
139 * (see @grand_byte@).
142 void grand_fill(grand
*r
, void *p
, size_t sz
)
144 if (r
->ops
->fill
!= grand_fill
)
145 r
->ops
->fill(r
, p
, sz
);
149 *q
++ = r
->ops
->byte(r
);
155 /*----- That's all, folks -------------------------------------------------*/