catcrypt.c: Don't close output file twice.
[u/mdw/catacomb] / lcrand.h
CommitLineData
07871354 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: lcrand.h,v 1.3 2004/04/08 01:36:15 mdw Exp $
07871354 4 *
5 * Simple linear congruential generator
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
07871354 11 *
12 * This file is part of Catacomb.
13 *
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.
45c0fd36 18 *
07871354 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.
45c0fd36 23 *
07871354 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,
27 * MA 02111-1307, USA.
28 */
29
07871354 30/*----- Notes on the linear congruential generator ------------------------*
31 *
32 * This pseudorandom number generator is simple, but has absolutely no
33 * cryptographic strength whatever. It may be used whenever random numbers
34 * are required but cryptographic strength is not, for example when
35 * generating numbers for use in primality tests. To be honest, it's not
36 * even particularly fast, although a certain amount of effort has been
bc4c10df 37 * expended on making it better than awfully slow. To put things in
38 * perspective, it can't quite spit bytes out as fast as OFB DES. (Then
39 * again, bytes aren't its natural output format.) Its main use is probably
40 * seeding a Fibonacci generator.
07871354 41 *
42 * There exists a fixed-point input @LCRAND_FIXEDPT@ -- when fed to the
43 * generator it comes straight back out again. All other inputs less than
44 * the modulus are part of the same sequence of period %$p - 1$%.
45 *
46 * The generator has been tested for its statistical properties. George
47 * Marsaglia's Diehard tests give it a reasonably clean bill of health.
48 *
49 * The modulus %$p$% is chosen as the largest prime number less than
50 * %$2^{32}$%. The multiplier %$a$% and additive constant %$c$% are based on
51 * the decimal expansions of %$\pi$% and %$e$%, with the additional
52 * restriction that the multiplier must be a primitive element modulo %$p$%.
53 * The fixed point value is determined as %$c / (1 - a) \bmod p$%.
54 */
55
bc4c10df 56#ifndef CATACOMB_LCRAND_H
57#define CATACOMB_LCRAND_H
58
59#ifdef __cplusplus
60 extern "C" {
61#endif
62
07871354 63/*----- Header files ------------------------------------------------------*/
64
65#include <mLib/bits.h>
66
67#ifndef CATACOMB_GRAND_H
68# include "grand.h"
69#endif
70
71/*----- Constants ---------------------------------------------------------*/
72
73#define LCRAND_P 4294967291u /* Modulus for the generator */
74#define LCRAND_A 314159265u /* Multiplier (primitive mod @p@) */
75#define LCRAND_C 271828183u /* Additive constant */
76
77#define LCRAND_FIXEDPT 3223959250u /* Fixed point (only bad input) */
78
79/*----- Functions provided ------------------------------------------------*/
80
81/* --- @lcrand@ --- *
82 *
83 * Arguments: @uint32 x@ = seed value
84 *
85 * Returns: New state of the generator.
86 *
87 * Use: Steps the generator. Returns %$ax + c \bmod p$%.
88 */
89
90extern uint32 lcrand(uint32 /*x*/);
91
92/* --- @lcrand_range@ --- *
93 *
94 * Arguments: @uint32 *x@ = pointer to seed value (updated)
95 * @uint32 m@ = limit allowable
96 *
97 * Returns: A uniformly distributed pseudorandom integer in the interval
98 * %$[0, m)$%.
99 */
100
101extern uint32 lcrand_range(uint32 */*x*/, uint32 /*m*/);
102
103/* --- @lcrand_create@ --- *
104 *
105 * Arguments: @uint32 x@ = initial seed
106 *
107 * Returns: Pointer to a generic generator.
108 *
109 * Use: Constructs a generic generator interface over a linear
110 * congruential generator.
111 */
112
113extern grand *lcrand_create(uint32 /*x*/);
114
115/*----- That's all, folks -------------------------------------------------*/
116
117#ifdef __cplusplus
118 }
119#endif
120
121#endif