3 * Simple linear congruential generator
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 ------------------------------------------------------*/
35 #include <mLib/bits.h>
41 /*----- Magic numbers -----------------------------------------------------*/
43 /* --- The generator parameters --- */
45 #define P LCRAND_P /* Modulus */
46 #define A LCRAND_A /* Multiplier (primitive mod @p@) */
47 #define C LCRAND_C /* Additive constant */
49 /* --- Precomputed values for modular reduction --- */
51 #define D 5 /* %$p = 2^{32} - d$% */
53 /* --- Other useful bits --- */
55 #define P256 4294967040u /* Highest multiple of 256 < %$p$% */
57 /*----- Main code ---------------------------------------------------------*/
61 * Arguments: @uint32 x@ = seed value
63 * Returns: New state of the generator.
65 * Use: Steps the generator. Returns %$ax + c \bmod p$%.
68 uint32 lcrand(uint32 x)
73 /* --- Unpack things into the arrays --- */
75 a[0] = U16(A); a[1] = U16(A >> 16);
76 xx[0] = U16(x); xx[1] = U16(x >> 16);
78 /* --- Multiply everything together --- *
80 * This is plain old long multiplication, although it looks a bit strange.
81 * I set up the top and bottom partial products directly where they're
82 * supposed to be. The cross terms I add together, with the low 16 bits in
83 * @q@ and the high 32 bits in @p@. These I then add into the product.
94 p = ((q < p) << 16) + (q >> 16);
101 p += (q >> 16) >> 16;
107 /* --- Now reduce mod p --- *
109 * I'm using shifts and adds to do the multiply step here. This needs to
110 * be changed if @D@ ever becomes something other than 5.
114 # error "Change shift sequence!"
131 y += (q >> 16) >> 16;
139 /* --- Now add on the constant --- */
150 /* --- @lcrand_range@ --- *
152 * Arguments: @uint32 *x@ = pointer to seed value (updated)
153 * @uint32 m@ = limit allowable
155 * Returns: A uniformly distributed pseudorandom integer in the interval
159 uint32 lcrand_range(uint32 *x, uint32 m)
162 uint32 r = P - P % m;
163 do xx = lcrand(xx); while (xx >= r);
168 /*----- Generic interface -------------------------------------------------*/
170 typedef struct gctx {
175 static void gdestroy(grand *r)
181 static int gmisc(grand *r, unsigned op, ...)
190 switch (va_arg(ap, unsigned)) {
193 case GRAND_SEEDUINT32:
203 g->x = va_arg(ap, unsigned);
205 case GRAND_SEEDUINT32:
206 g->x = va_arg(ap, uint32);
208 case GRAND_SEEDRAND: {
209 grand *rr = va_arg(ap, grand *);
211 do x = rr->ops->word(rr); while (x >= P || x == LCRAND_FIXEDPT);
223 static uint32 graw(grand *r)
230 static octet gbyte(grand *r)
234 do x = lcrand(x); while (x >= P256);
236 return (x / (P256 / 256));
239 static uint32 grange(grand *r, uint32 l)
242 return (lcrand_range(&g->x, l));
245 static const grand_ops gops = {
249 graw, gbyte, grand_word, grange, grand_fill
252 /* --- @lcrand_create@ --- *
254 * Arguments: @uint32 x@ = initial seed
256 * Returns: Pointer to a generic generator.
258 * Use: Constructs a generic generator interface over a linear
259 * congruential generator.
262 grand *lcrand_create(uint32 x)
264 gctx *g = CREATE(gctx);
270 /*----- Test rig ----------------------------------------------------------*/
274 #include <mLib/testrig.h>
276 static int verify(dstr *v)
278 uint32 x = *(uint32 *)v[0].buf;
279 uint32 y = *(uint32 *)v[1].buf;
280 uint32 z = lcrand(x);
284 "\n*** lcrand failed. lcrand(%lu) = %lu, expected %lu\n",
285 (unsigned long)x, (unsigned long)z, (unsigned long)y);
291 static test_chunk tests[] = {
292 { "lcrand", verify, { &type_uint32, &type_uint32, 0 } },
296 int main(int argc, char *argv[])
298 test_run(argc, argv, tests, SRCDIR"/t/lcrand");
304 /*----- That's all, folks -------------------------------------------------*/