/* -*-c-*-
*
* $Id: lcrand.h,v 1.2 2000/07/02 15:21:20 mdw Exp $
*
* Simple linear congruential generator
*
* (c) 1999 Straylight/Edgeware
* (c) 2000 Mark Wooding
*/
/*----- Licensing notice --------------------------------------------------*
*
* Copyright (c) 2000 Mark Wooding
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2, Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The name of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Instead of accepting the above terms, you may redistribute and/or modify
* this software under the terms of either the GNU General Public License,
* or the GNU Library General Public License, published by the Free
* Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*/
/*----- Revision history --------------------------------------------------*
*
* $Log: lcrand.h,v $
* Revision 1.2 2000/07/02 15:21:20 mdw
* Fix licence text.
*
* Revision 1.1 2000/05/21 11:28:30 mdw
* Initial check-in.
*
* --- Past lives (Catacomb) --- *
*
* Revision 1.1 1999/12/10 23:15:27 mdw
* Noncryptographic random number generator.
*
*/
/*----- Notes on the linear congruential generator ------------------------*
*
* This pseudorandom number generator is simple, but has absolutely no
* cryptographic strength whatever. It may be used whenever random numbers
* are required but cryptographic strength is not, for example when
* generating numbers for use in primality tests. To be honest, it's not
* even particularly fast, although a certain amount of effort has been
* expended on making it better than awfully slow. To put things in
* perspective, it can't quite spit bytes out as fast as OFB DES. (Then
* again, bytes aren't its natural output format.) Its main use is probably
* seeding a Fibonacci generator.
*
* There exists a fixed-point input @LCRAND_FIXEDPT@ -- when fed to the
* generator it comes straight back out again. All other inputs less than
* the modulus are part of the same sequence of period %$p - 1$%.
*
* The generator has been tested for its statistical properties. George
* Marsaglia's Diehard tests give it a reasonably clean bill of health.
*
* The modulus %$p$% is chosen as the largest prime number less than
* %$2^{32}$%. The multiplier %$a$% and additive constant %$c$% are based on
* the decimal expansions of %$\pi$% and %$e$%, with the additional
* restriction that the multiplier must be a primitive element modulo %$p$%.
* The fixed point value is determined as %$c / (1 - a) \bmod p$%.
*/
#ifndef LCRAND_H
#define LCRAND_H
#ifdef __cplusplus
extern "C" {
#endif
/*----- Header files ------------------------------------------------------*/
#ifndef BITS_H
# include "bits.h"
#endif
/*----- Constants ---------------------------------------------------------*/
#define LCRAND_P 4294967291u /* Modulus for the generator */
#define LCRAND_A 314159265u /* Multiplier (primitive mod @p@) */
#define LCRAND_C 271828183u /* Additive constant */
#define LCRAND_FIXEDPT 3223959250u /* Fixed point (only bad input) */
/*----- Functions provided ------------------------------------------------*/
/* --- @lcrand@ --- *
*
* Arguments: @uint32 x@ = seed value
*
* Returns: New state of the generator.
*
* Use: Steps the generator. Returns %$ax + c \bmod p$%.
*/
extern uint32 lcrand(uint32 /*x*/);
/* --- @lcrand_range@ --- *
*
* Arguments: @uint32 *x@ = pointer to seed value (updated)
* @uint32 m@ = limit allowable
*
* Returns: A uniformly distributed pseudorandom integer in the interval
* %$[0, m)$%.
*/
extern uint32 lcrand_range(uint32 */*x*/, uint32 /*m*/);
/*----- That's all, folks -------------------------------------------------*/
#ifdef __cplusplus
}
#endif
#endif