3 * $Id: lcrand.c,v 1.2 1999/12/13 15:34:01 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.2 1999/12/13 15:34:01 mdw
34 * Add support for seeding from a generic pseudorandom source.
36 * Revision 1.1 1999/12/10 23:15:27 mdw
37 * Noncryptographic random number generator.
41 /*----- Header files ------------------------------------------------------*/
48 #include <mLib/bits.h>
54 /*----- Magic numbers -----------------------------------------------------*/
56 /* --- The generator parameters --- */
58 #define P LCRAND_P /* Modulus */
59 #define A LCRAND_A /* Multiplier (primitive mod @p@) */
60 #define C LCRAND_C /* Additive constant */
62 /* --- Precomputed values for modular reduction --- */
64 #define D 5 /* %$p = 2^{32} - d$% */
66 /* --- Other useful bits --- */
68 #define P256 4294967040u /* Highest multiple of 256 < %$p$% */
70 /*----- Main code ---------------------------------------------------------*/
74 * Arguments: @uint32 x@ = seed value
76 * Returns: New state of the generator.
78 * Use: Steps the generator. Returns %$ax + c \bmod p$%.
81 uint32
lcrand(uint32 x
)
86 /* --- Unpack things into the arrays --- */
88 a
[0] = U16(A
); a
[1] = U16(A
>> 16);
89 xx
[0] = U16(x
); xx
[1] = U16(x
>> 16);
91 /* --- Multiply everything together --- *
93 * This is plain old long multiplication, although it looks a bit strange.
94 * I set up the top and bottom partial products directly where they're
95 * supposed to be. The cross terms I add together, with the low 16 bits in
96 * @q@ and the high 32 bits in @p@. These I then add into the product.
102 yy
[0] = a
[0] * xx
[0];
103 yy
[1] = a
[1] * xx
[1];
106 q
= p
+ a
[1] * xx
[0];
107 p
= ((q
< p
) << 16) + (q
>> 16);
114 p
+= (q
>> 16) >> 16;
120 /* --- Now reduce mod p --- *
122 * I'm using shifts and adds to do the multiply step here. This needs to
123 * be changed if @D@ ever becomes something other than 5.
127 # error "Change shift sequence!"
144 y
+= (q
>> 16) >> 16;
152 /* --- Now add on the constant --- */
163 /* --- @lcrand_range@ --- *
165 * Arguments: @uint32 *x@ = pointer to seed value (updated)
166 * @uint32 m@ = limit allowable
168 * Returns: A uniformly distributed pseudorandom integer in the interval
172 uint32
lcrand_range(uint32
*x
, uint32 m
)
175 uint32 r
= P
- P
% m
;
176 do xx
= lcrand(xx
); while (xx
>= r
);
178 return (xx
/ (r
/ m
));
181 /*----- Generic interface -------------------------------------------------*/
183 typedef struct gctx
{
188 static void gdestroy(grand
*r
)
194 static int gmisc(grand
*r
, unsigned op
, ...)
203 switch (va_arg(ap
, unsigned)) {
206 case GRAND_SEEDUINT32
:
216 g
->x
= va_arg(ap
, unsigned);
218 case GRAND_SEEDUINT32
:
219 g
->x
= va_arg(ap
, uint32
);
221 case GRAND_SEEDRAND
: {
222 grand
*rr
= va_arg(ap
, grand
*);
224 do x
= rr
->ops
->word(rr
); while (x
>= P
|| x
== LCRAND_FIXEDPT
);
236 static uint32
graw(grand
*r
)
243 static octet
gbyte(grand
*r
)
247 do x
= lcrand(x
); while (x
>= P256
);
249 return (x
/ (P256
/ 256));
252 static uint32
grange(grand
*r
, uint32 l
)
255 return (lcrand_range(&g
->x
, l
));
258 static const grand_ops gops
= {
262 graw
, gbyte
, grand_word
, grange
, grand_fill
265 /* --- @lcrand_create@ --- *
267 * Arguments: @uint32 x@ = initial seed
269 * Returns: Pointer to a generic generator.
271 * Use: Constructs a generic generator interface over a linear
272 * congruential generator.
275 grand
*lcrand_create(uint32 x
)
277 gctx
*g
= CREATE(gctx
);
283 /*----- Test rig ----------------------------------------------------------*/
287 #include <mLib/testrig.h>
289 static int verify(dstr
*v
)
291 uint32 x
= *(uint32
*)v
[0].buf
;
292 uint32 y
= *(uint32
*)v
[1].buf
;
293 uint32 z
= lcrand(x
);
297 "\n*** lcrand failed. lcrand(%lu) = %lu, expected %lu\n",
298 (unsigned long)x
, (unsigned long)z
, (unsigned long)y
);
304 static test_chunk tests
[] = {
305 { "lcrand", verify
, { &type_uint32
, &type_uint32
, 0 } },
309 int main(int argc
, char *argv
[])
311 test_run(argc
, argv
, tests
, SRCDIR
"/tests/lcrand");
317 /*----- That's all, folks -------------------------------------------------*/