Initial revision
[LeanCalc.git] / calc / calc / zrandom.h
1 /*
2  * zrandom - Blum-Blum-Shub pseudo-random generator
3  *
4  * Copyright (C) 1999  Landon Curt Noll
5  *
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.
9  *
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.
14  *
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.
19  *
20  * @(#) $Revision$
21  * @(#) $Id$
22  * @(#) $Source$
23  *
24  * Under source code control:   1997/02/15 04:01:56
25  * File existed as early as:    1997
26  *
27  * chongo <was here> /\oo/\     http://www.isthe.com/chongo/
28  * Share and enjoy!  :-)        http://www.isthe.com/chongo/tech/comp/calc/
29  */
30
31
32 #if !defined(__ZRANDOM_H__)
33 #define __ZRANDOM_H__
34
35
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"
39 #else
40 # include <calc/value.h>
41 # include <calc/have_const.h>
42 #endif
43
44
45 /*
46  * Blum generator state
47  *
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.
56  */
57 struct random {
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 */
65 };
66
67
68 /*
69  * Blum constants
70  */
71 #define BLUM_PREGEN 20  /* number of non-default predefined Blum generators */
72
73
74 /*
75  * Blum generator function declarations
76  */
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);
92
93
94 #endif /* !__ZRANDOM_H__ */