2 * zrandom - Blum-Blum-Shub pseudo-random generator
4 * Copyright (C) 1999-2007 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.1 $
21 * @(#) $Id: zrandom.h,v 30.1 2007/03/16 11:09:46 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/zrandom.h,v $
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 */
38 # include "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 E_FUNC RANDOM
*zsrandom1(CONST ZVALUE seed
, BOOL need_ret
);
78 E_FUNC RANDOM
*zsrandom2(CONST ZVALUE seed
, CONST ZVALUE newn
);
79 E_FUNC RANDOM
*zsrandom4(CONST ZVALUE seed
,
80 CONST ZVALUE ip
, CONST ZVALUE iq
, long trials
);
81 E_FUNC RANDOM
*zsetrandom(CONST RANDOM
*state
);
82 E_FUNC
void zrandomskip(long count
);
83 E_FUNC
void zrandom(long count
, ZVALUE
*res
);
84 E_FUNC
void zrandom(long count
, ZVALUE
*res
);
85 E_FUNC
void zrandomrange(CONST ZVALUE low
, CONST ZVALUE beyond
, ZVALUE
*res
);
86 E_FUNC
long irandom(long s
);
87 E_FUNC RANDOM
*randomcopy(CONST RANDOM
*random
);
88 E_FUNC
void randomfree(RANDOM
*random
);
89 E_FUNC BOOL
randomcmp(CONST RANDOM
*s1
, CONST RANDOM
*s2
);
90 E_FUNC
void randomprint(CONST RANDOM
*state
, int flags
);
91 E_FUNC
void random_libcalc_cleanup(void);
94 #endif /* !__ZRANDOM_H__ */