2 * zrandom - Blum-Blum-Shub pseudo-random generator
4 * Copyright (C) 1999 Landon Curt Noll
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24 * Under source code control: 1997/02/15 04:01:56
25 * File existed as early as: 1997
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 #if !defined(__ZRANDOM_H__)
36 #if defined(CALC_SRC) /* if we are building from the calc source tree */
37 # include "calc/value.h"
38 # include "calc/have_const.h"
40 # include <calc/value.h>
41 # include <calc/have_const.h>
46 * Blum generator state
48 * The size of the buffer implies that a turn of the quadratic residue crank
49 * will never yield as many at the than the number of bits in a HALF. At
50 * most this implies that a turn can yield no more than 15 bits when BASEB==16
51 * or 31 bits when BASEB==32. Should we deal with a excessively large
52 * Blum modulus (>=2^16 bits long for BASEB==16, >=2^32 bits for BASEB==32)
53 * the higher order random bits will be tossed. This is not a loss as
54 * regular sub-segments of the sequence are just as random. It only means
55 * that excessively large Blum modulus values waste CPU time.
58 int seeded; /* 1 => state has been seeded */
59 int bits; /* number of unused bits in buffer */
60 int loglogn; /* int(log2(log2(n))), bits produced per turn */
61 HALF buffer; /* unused random bits from previous call */
62 HALF mask; /* mask for the log2(log2(n)) lower bits of r */
63 ZVALUE n; /* Blum modulus */
64 ZVALUE r; /* Blum quadratic residue */
71 #define BLUM_PREGEN 20 /* number of non-default predefined Blum generators */
75 * Blum generator function declarations
77 extern RANDOM *zsrandom1(CONST ZVALUE seed, BOOL need_ret);
78 extern RANDOM *zsrandom2(CONST ZVALUE seed, CONST ZVALUE newn);
79 extern RANDOM *zsrandom4(CONST ZVALUE seed,
80 CONST ZVALUE ip, CONST ZVALUE iq, long trials);
81 extern RANDOM *zsetrandom(CONST RANDOM *state);
82 extern void zrandomskip(long count);
83 extern void zrandom(long count, ZVALUE *res);
84 extern void zrandom(long count, ZVALUE *res);
85 extern void zrandomrange(CONST ZVALUE low, CONST ZVALUE beyond, ZVALUE *res);
86 extern long irandom(long s);
87 extern RANDOM *randomcopy(CONST RANDOM *random);
88 extern void randomfree(RANDOM *random);
89 extern BOOL randomcmp(CONST RANDOM *s1, CONST RANDOM *s2);
90 extern void randomprint(CONST RANDOM *state, int flags);
91 extern void random_libcalc_cleanup(void);
94 #endif /* !__ZRANDOM_H__ */