3 * $Id: mprand.c,v 1.2 1999/12/22 15:55:33 mdw Exp $
5 * Generate a random multiprecision integer
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/22 15:55:33 mdw
34 * Modify `mprand' slightly. Add `mprand_range'.
36 * Revision 1.1 1999/12/10 23:23:05 mdw
37 * Support for generating random large integers.
41 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/alloc.h>
49 /*----- Main code ---------------------------------------------------------*/
53 * Arguments: @mp *d@ = destination integer
54 * @unsigned b@ = number of bits
55 * @grand *r@ = pointer to random number source
56 * @mpw or@ = mask to OR with low-order bits
58 * Returns: A random integer with the requested number of bits.
60 * Use: Constructs an arbitrarily large pseudorandom integer.
61 * Assuming that the generator @r@ is good, the result is
62 * uniformly distributed in the interval %$[2^{b - 1}, 2^b)$%.
63 * The result is then ORred with the given @or@ value. This
64 * will often be 1, to make the result odd.
67 mp
*mprand(mp
*d
, unsigned b
, grand
*r
, mpw
or)
69 size_t sz
= (b
+ 7) >> 3;
70 octet
*v
= xmalloc(sz
);
73 /* --- Fill buffer with random data --- */
75 r
->ops
->fill(r
, v
, sz
);
77 /* --- Force into the correct range --- *
79 * This is slightly tricky. Oh, well.
84 v
[0] = (v
[0] & (m
- 1)) | m
;
86 /* --- Mask, load and return --- */
88 d
= mp_loadb(d
, v
, sz
);
94 /* --- @mprand_range@ --- *
96 * Arguments: @mp *d@ = destination integer
97 * @mp *l@ = limit for random number
98 * @grand *r@ = random number source
99 * @mpw or@ = mask for low-order bits
101 * Returns: A pseudorandom integer, unformly distributed over the
102 * interval %$[0, l)$%.
104 * Use: Generates a uniformly-distributed pseudorandom number in the
108 mp
*mprand_range(mp
*d
, mp
*l
, grand
*r
, mpw
or)
110 size_t b
= mp_bits(l
);
111 size_t sz
= (b
+ 7) >> 3;
112 octet
*v
= xmalloc(sz
);
115 /* --- The algorithm --- *
117 * Rather simpler than most. Find the number of bits in the number %$l$%
118 * (i.e., the integer %$b$% such that %$2^{b - 1} \le l < 2^b$%), and
119 * generate pseudorandom integers with %$n$% bits (but not, unlike in the
120 * function above, with the top bit forced to 1). If the integer is
121 * greater than or equal to %$l$%, try again.
123 * This is similar to the algorithms used in @lcrand_range@ and friends,
124 * except that I've forced the `raw' range of the random numbers such that
125 * %$l$% itself is the largest multiple of %$l$% in the range (since, by
126 * the inequality above, %$2^b \le 2l$%). This removes the need for costly
127 * division and remainder operations.
129 * As usual, the number of iterations expected is two.
135 r
->ops
->fill(r
, v
, sz
);
137 d
= mp_loadb(d
, v
, sz
);
139 } while (MP_CMP(d
, >=, l
));
147 /*----- That's all, folks -------------------------------------------------*/