3 * $Id: grand.h,v 1.1 1999/12/10 23:16:01 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.1 1999/12/10 23:16:01 mdw
38 #ifndef CATACOMB_GRAND_H
39 #define CATACOMB_GRAND_H
45 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/bits.h>
52 /*----- Generic random number generator interface -------------------------*/
54 typedef struct grand
{
55 const struct grand_ops
*ops
;
58 typedef struct grand_ops
{
60 /* --- Various important properties --- */
62 const char *name
; /* Generator's name */
63 uint32 max
; /* Maximum raw output */
65 /* --- Maintenance methods --- */
67 int (*misc
)(grand */
*r*/
, unsigned /*op*/, ...); /* Miscellaneous ops */
68 void (*destroy
)(grand */
*r*/
); /* Destroy generator context */
70 /* --- Output methods --- *
72 * Only one of these operations need actually be implemented. All the
73 * other operations may be synthesized. Of course, performance is improved
74 * if more are provided.
77 uint32 (*raw
)(grand */
*r*/
); /* Uniform over %$[0, max)$% */
78 octet (*byte
)(grand */
*r*/
); /* Uniform over %$[0, 256)%$ */
79 uint32 (*word
)(grand */
*r*/
); /* Uniform over %$[0, 2^{32})$% */
80 uint32 (*range
)(grand */
*r*/
, uint32
/*l*/); /* Uniform over %$[0, l)$% */
81 void (*fill
)(grand */
*r*/
, void */
*p*/
, size_t /*sz*/); /* Fill buffer */
84 /* --- Operation types --- */
88 /* --- Required operations --- */
90 GRAND_CHECK
, /* @unsigned op2@ */
92 /* --- Standard seeding operations --- */
94 GRAND_SEEDINT
, /* @int i@ */
95 GRAND_SEEDUINT32
, /* @uint32 i@ */
96 GRAND_SEEDBLOCK
, /* @const void *p, size_t sz@ */
97 GRAND_SEEDMP
, /* @mp *m@ */
98 GRAND_SEEDRAND
, /* @grand *g@ */
100 /* --- Generator-specific operations --- */
102 GRAND_SPECIFIC
= 256u
105 #define GRAND_BADOP assert(((void)"bad grand_misc op", 0))
107 /*----- Functions provided ------------------------------------------------*/
109 /* --- @grand_byte@ --- *
111 * Arguments: @grand *r@ = pointet to generic generator
113 * Returns: A uniformly-distributed pseudorandom integer in the interval
117 extern octet
grand_byte(grand */
*r*/
);
119 /* --- @grand_word@ --- *
121 * Arguments: @grand *r@ = pointet to generic generator
123 * Returns: A uniformly-distributed pseudorandom integer in the interval
127 extern uint32
grand_word(grand */
*r*/
);
129 /* --- @grand_range@ --- *
131 * Arguments: @grand *r@ = pointet to generic generator
132 * @uint32 l@ = limit for acceptable results
134 * Returns: A uniformly-distributed pseudorandom integer in the interval
138 extern uint32
grand_range(grand */
*r*/
, uint32
/*l*/);
140 /* --- @grand_fill@ --- *
142 * Arguments: @grand *r@ = pointet to generic generator
143 * @void *p@ = pointer to a buffer
144 * @size_t sz@ = size of the buffer
148 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
149 * (see @grand_byte@).
152 extern void grand_fill(grand */
*r*/
, void */
*p*/
, size_t /*sz*/);
154 /*----- That's all, folks -------------------------------------------------*/