3 * $Id: lcrand.c,v 1.5 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
43 /*----- Magic numbers -----------------------------------------------------*/
45 /* --- The generator parameters --- */
47 #define P LCRAND_P /* Modulus */
48 #define A LCRAND_A /* Multiplier (primitive mod @p@) */
49 #define C LCRAND_C /* Additive constant */
51 /* --- Precomputed values for modular reduction --- */
53 #define D 5 /* %$p = 2^{32} - d$% */
55 /* --- Other useful bits --- */
57 #define P256 4294967040u /* Highest multiple of 256 < %$p$% */
59 /*----- Main code ---------------------------------------------------------*/
63 * Arguments: @uint32 x@ = seed value
65 * Returns: New state of the generator.
67 * Use: Steps the generator. Returns %$ax + c \bmod p$%.
70 uint32 lcrand(uint32 x)
75 /* --- Unpack things into the arrays --- */
77 a[0] = U16(A); a[1] = U16(A >> 16);
78 xx[0] = U16(x); xx[1] = U16(x >> 16);
80 /* --- Multiply everything together --- *
82 * This is plain old long multiplication, although it looks a bit strange.
83 * I set up the top and bottom partial products directly where they're
84 * supposed to be. The cross terms I add together, with the low 16 bits in
85 * @q@ and the high 32 bits in @p@. These I then add into the product.
96 p = ((q < p) << 16) + (q >> 16);
103 p += (q >> 16) >> 16;
109 /* --- Now reduce mod p --- *
111 * I'm using shifts and adds to do the multiply step here. This needs to
112 * be changed if @D@ ever becomes something other than 5.
116 # error "Change shift sequence!"
133 y += (q >> 16) >> 16;
141 /* --- Now add on the constant --- */
152 /* --- @lcrand_range@ --- *
154 * Arguments: @uint32 *x@ = pointer to seed value (updated)
155 * @uint32 m@ = limit allowable
157 * Returns: A uniformly distributed pseudorandom integer in the interval
161 uint32 lcrand_range(uint32 *x, uint32 m)
164 uint32 r = P - P % m;
165 do xx = lcrand(xx); while (xx >= r);
170 /*----- Generic interface -------------------------------------------------*/
172 typedef struct gctx {
177 static void gdestroy(grand *r)
183 static int gmisc(grand *r, unsigned op, ...)
192 switch (va_arg(ap, unsigned)) {
195 case GRAND_SEEDUINT32:
205 g->x = va_arg(ap, unsigned);
207 case GRAND_SEEDUINT32:
208 g->x = va_arg(ap, uint32);
210 case GRAND_SEEDRAND: {
211 grand *rr = va_arg(ap, grand *);
213 do x = rr->ops->word(rr); while (x >= P || x == LCRAND_FIXEDPT);
225 static uint32 graw(grand *r)
232 static octet gbyte(grand *r)
236 do x = lcrand(x); while (x >= P256);
238 return (x / (P256 / 256));
241 static uint32 grange(grand *r, uint32 l)
244 return (lcrand_range(&g->x, l));
247 static const grand_ops gops = {
251 graw, gbyte, grand_word, grange, grand_fill
254 /* --- @lcrand_create@ --- *
256 * Arguments: @uint32 x@ = initial seed
258 * Returns: Pointer to a generic generator.
260 * Use: Constructs a generic generator interface over a linear
261 * congruential generator.
264 grand *lcrand_create(uint32 x)
266 gctx *g = CREATE(gctx);
272 /*----- Test rig ----------------------------------------------------------*/
276 #include <mLib/testrig.h>
278 static int verify(dstr *v)
280 uint32 x = *(uint32 *)v[0].buf;
281 uint32 y = *(uint32 *)v[1].buf;
282 uint32 z = lcrand(x);
286 "\n*** lcrand failed. lcrand(%lu) = %lu, expected %lu\n",
287 (unsigned long)x, (unsigned long)z, (unsigned long)y);
293 static test_chunk tests[] = {
294 { "lcrand", verify, { &type_uint32, &type_uint32, 0 } },
298 int main(int argc, char *argv[])
300 test_run(argc, argv, tests, SRCDIR"/tests/lcrand");
306 /*----- That's all, folks -------------------------------------------------*/