3 * $Id: lcrand.c,v 1.1 1999/12/10 23:15:27 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.1 1999/12/10 23:15:27 mdw
34 * Noncryptographic random number generator.
38 /*----- Header files ------------------------------------------------------*/
45 #include <mLib/bits.h>
51 /*----- Magic numbers -----------------------------------------------------*/
53 /* --- The generator parameters --- */
55 #define P LCRAND_P /* Modulus */
56 #define A LCRAND_A /* Multiplier (primitive mod @p@) */
57 #define C LCRAND_C /* Additive constant */
59 /* --- Precomputed values for modular reduction --- */
61 #define D 5 /* %$p = 2^{32} - d$% */
63 /* --- Other useful bits --- */
65 #define P256 4294967040u /* Highest multiple of 256 < %$p$% */
67 /*----- Main code ---------------------------------------------------------*/
71 * Arguments: @uint32 x@ = seed value
73 * Returns: New state of the generator.
75 * Use: Steps the generator. Returns %$ax + c \bmod p$%.
78 uint32
lcrand(uint32 x
)
83 /* --- Unpack things into the arrays --- */
85 a
[0] = U16(A
); a
[1] = U16(A
>> 16);
86 xx
[0] = U16(x
); xx
[1] = U16(x
>> 16);
88 /* --- Multiply everything together --- *
90 * This is plain old long multiplication, although it looks a bit strange.
91 * I set up the top and bottom partial products directly where they're
92 * supposed to be. The cross terms I add together, with the low 16 bits in
93 * @q@ and the high 32 bits in @p@. These I then add into the product.
100 yy
[1] = a
[1] * xx
[1];
103 q
= p
+ a
[1] * xx
[0];
104 p
= ((q
< p
) << 16) + (q
>> 16);
111 p
+= (q
>> 16) >> 16;
117 /* --- Now reduce mod p --- *
119 * I'm using shifts and adds to do the multiply step here. This needs to
120 * be changed if @D@ ever becomes something other than 5.
124 # error "Change shift sequence!"
141 y
+= (q
>> 16) >> 16;
149 /* --- Now add on the constant --- */
160 /* --- @lcrand_range@ --- *
162 * Arguments: @uint32 *x@ = pointer to seed value (updated)
163 * @uint32 m@ = limit allowable
165 * Returns: A uniformly distributed pseudorandom integer in the interval
169 uint32
lcrand_range(uint32
*x
, uint32 m
)
172 uint32 r
= P
- P
% m
;
173 do xx
= lcrand(xx
); while (xx
>= r
);
175 return (xx
/ (r
/ m
));
178 /*----- Generic interface -------------------------------------------------*/
180 typedef struct gctx
{
185 static void gdestroy(grand
*r
)
191 static int gmisc(grand
*r
, unsigned op
, ...)
200 switch (va_arg(ap
, unsigned)) {
203 case GRAND_SEEDUINT32
:
212 g
->x
= va_arg(ap
, unsigned);
214 case GRAND_SEEDUINT32
:
215 g
->x
= va_arg(ap
, uint32
);
226 static uint32
graw(grand
*r
)
233 static octet
gbyte(grand
*r
)
237 do x
= lcrand(x
); while (x
>= P256
);
239 return (x
/ (P256
/ 256));
242 static uint32
grange(grand
*r
, uint32 l
)
245 return (lcrand_range(&g
->x
, l
));
248 static const grand_ops gops
= {
252 graw
, gbyte
, grand_word
, grange
, grand_fill
255 /* --- @lcrand_create@ --- *
257 * Arguments: @uint32 x@ = initial seed
259 * Returns: Pointer to a generic generator.
261 * Use: Constructs a generic generator interface over a linear
262 * congruential generator.
265 grand
*lcrand_create(uint32 x
)
267 gctx
*g
= CREATE(gctx
);
273 /*----- Test rig ----------------------------------------------------------*/
277 #include <mLib/testrig.h>
279 static int verify(dstr
*v
)
281 uint32 x
= *(uint32
*)v
[0].buf
;
282 uint32 y
= *(uint32
*)v
[1].buf
;
283 uint32 z
= lcrand(x
);
287 "\n*** lcrand failed. lcrand(%lu) = %lu, expected %lu\n",
288 (unsigned long)x
, (unsigned long)z
, (unsigned long)y
);
294 static test_chunk tests
[] = {
295 { "lcrand", verify
, { &type_uint32
, &type_uint32
, 0 } },
299 int main(int argc
, char *argv
[])
301 test_run(argc
, argv
, tests
, SRCDIR
"/tests/lcrand");
307 /*----- That's all, folks -------------------------------------------------*/