3 * $Id: lcrand.c,v 1.3 2000/06/17 11:29:03 mdw Exp $
5 * Simple linear congruential generator
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.3 2000/06/17 11:29:03 mdw
34 * Add the flags word to the generic generator.
36 * Revision 1.2 1999/12/13 15:34:01 mdw
37 * Add support for seeding from a generic pseudorandom source.
39 * Revision 1.1 1999/12/10 23:15:27 mdw
40 * Noncryptographic random number generator.
44 /*----- Header files ------------------------------------------------------*/
51 #include <mLib/bits.h>
57 /*----- Magic numbers -----------------------------------------------------*/
59 /* --- The generator parameters --- */
61 #define P LCRAND_P /* Modulus */
62 #define A LCRAND_A /* Multiplier (primitive mod @p@) */
63 #define C LCRAND_C /* Additive constant */
65 /* --- Precomputed values for modular reduction --- */
67 #define D 5 /* %$p = 2^{32} - d$% */
69 /* --- Other useful bits --- */
71 #define P256 4294967040u /* Highest multiple of 256 < %$p$% */
73 /*----- Main code ---------------------------------------------------------*/
77 * Arguments: @uint32 x@ = seed value
79 * Returns: New state of the generator.
81 * Use: Steps the generator. Returns %$ax + c \bmod p$%.
84 uint32 lcrand(uint32 x)
89 /* --- Unpack things into the arrays --- */
91 a[0] = U16(A); a[1] = U16(A >> 16);
92 xx[0] = U16(x); xx[1] = U16(x >> 16);
94 /* --- Multiply everything together --- *
96 * This is plain old long multiplication, although it looks a bit strange.
97 * I set up the top and bottom partial products directly where they're
98 * supposed to be. The cross terms I add together, with the low 16 bits in
99 * @q@ and the high 32 bits in @p@. These I then add into the product.
105 yy[0] = a[0] * xx[0];
106 yy[1] = a[1] * xx[1];
109 q = p + a[1] * xx[0];
110 p = ((q < p) << 16) + (q >> 16);
117 p += (q >> 16) >> 16;
123 /* --- Now reduce mod p --- *
125 * I'm using shifts and adds to do the multiply step here. This needs to
126 * be changed if @D@ ever becomes something other than 5.
130 # error "Change shift sequence!"
147 y += (q >> 16) >> 16;
155 /* --- Now add on the constant --- */
166 /* --- @lcrand_range@ --- *
168 * Arguments: @uint32 *x@ = pointer to seed value (updated)
169 * @uint32 m@ = limit allowable
171 * Returns: A uniformly distributed pseudorandom integer in the interval
175 uint32 lcrand_range(uint32 *x, uint32 m)
178 uint32 r = P - P % m;
179 do xx = lcrand(xx); while (xx >= r);
181 return (xx / (r / m));
184 /*----- Generic interface -------------------------------------------------*/
186 typedef struct gctx {
191 static void gdestroy(grand *r)
197 static int gmisc(grand *r, unsigned op, ...)
206 switch (va_arg(ap, unsigned)) {
209 case GRAND_SEEDUINT32:
219 g->x = va_arg(ap, unsigned);
221 case GRAND_SEEDUINT32:
222 g->x = va_arg(ap, uint32);
224 case GRAND_SEEDRAND: {
225 grand *rr = va_arg(ap, grand *);
227 do x = rr->ops->word(rr); while (x >= P || x == LCRAND_FIXEDPT);
239 static uint32 graw(grand *r)
246 static octet gbyte(grand *r)
250 do x = lcrand(x); while (x >= P256);
252 return (x / (P256 / 256));
255 static uint32 grange(grand *r, uint32 l)
258 return (lcrand_range(&g->x, l));
261 static const grand_ops gops = {
265 graw, gbyte, grand_word, grange, grand_fill
268 /* --- @lcrand_create@ --- *
270 * Arguments: @uint32 x@ = initial seed
272 * Returns: Pointer to a generic generator.
274 * Use: Constructs a generic generator interface over a linear
275 * congruential generator.
278 grand *lcrand_create(uint32 x)
280 gctx *g = CREATE(gctx);
286 /*----- Test rig ----------------------------------------------------------*/
290 #include <mLib/testrig.h>
292 static int verify(dstr *v)
294 uint32 x = *(uint32 *)v[0].buf;
295 uint32 y = *(uint32 *)v[1].buf;
296 uint32 z = lcrand(x);
300 "\n*** lcrand failed. lcrand(%lu) = %lu, expected %lu\n",
301 (unsigned long)x, (unsigned long)z, (unsigned long)y);
307 static test_chunk tests[] = {
308 { "lcrand", verify, { &type_uint32, &type_uint32, 0 } },
312 int main(int argc, char *argv[])
314 test_run(argc, argv, tests, SRCDIR"/tests/lcrand");
320 /*----- That's all, folks -------------------------------------------------*/