Chris@87: /* Random kit 1.3 */ Chris@87: Chris@87: /* Chris@87: * Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org) Chris@87: * Chris@87: * Permission is hereby granted, free of charge, to any person obtaining a Chris@87: * copy of this software and associated documentation files (the Chris@87: * "Software"), to deal in the Software without restriction, including Chris@87: * without limitation the rights to use, copy, modify, merge, publish, Chris@87: * distribute, sublicense, and/or sell copies of the Software, and to Chris@87: * permit persons to whom the Software is furnished to do so, subject to Chris@87: * the following conditions: Chris@87: * Chris@87: * The above copyright notice and this permission notice shall be included Chris@87: * in all copies or substantial portions of the Software. Chris@87: * Chris@87: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS Chris@87: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@87: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. Chris@87: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY Chris@87: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, Chris@87: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE Chris@87: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@87: */ Chris@87: Chris@87: /* @(#) $Jeannot: randomkit.h,v 1.24 2005/07/21 22:14:09 js Exp $ */ Chris@87: Chris@87: /* Chris@87: * Typical use: Chris@87: * Chris@87: * { Chris@87: * rk_state state; Chris@87: * unsigned long seed = 1, random_value; Chris@87: * Chris@87: * rk_seed(seed, &state); // Initialize the RNG Chris@87: * ... Chris@87: * random_value = rk_random(&state); // Generate random values in [0..RK_MAX] Chris@87: * } Chris@87: * Chris@87: * Instead of rk_seed, you can use rk_randomseed which will get a random seed Chris@87: * from /dev/urandom (or the clock, if /dev/urandom is unavailable): Chris@87: * Chris@87: * { Chris@87: * rk_state state; Chris@87: * unsigned long random_value; Chris@87: * Chris@87: * rk_randomseed(&state); // Initialize the RNG with a random seed Chris@87: * ... Chris@87: * random_value = rk_random(&state); // Generate random values in [0..RK_MAX] Chris@87: * } Chris@87: */ Chris@87: Chris@87: /* Chris@87: * Useful macro: Chris@87: * RK_DEV_RANDOM: the device used for random seeding. Chris@87: * defaults to "/dev/urandom" Chris@87: */ Chris@87: Chris@87: #include Chris@87: Chris@87: #ifndef _RANDOMKIT_ Chris@87: #define _RANDOMKIT_ Chris@87: Chris@87: #define RK_STATE_LEN 624 Chris@87: Chris@87: typedef struct rk_state_ Chris@87: { Chris@87: unsigned long key[RK_STATE_LEN]; Chris@87: int pos; Chris@87: int has_gauss; /* !=0: gauss contains a gaussian deviate */ Chris@87: double gauss; Chris@87: Chris@87: /* The rk_state structure has been extended to store the following Chris@87: * information for the binomial generator. If the input values of n or p Chris@87: * are different than nsave and psave, then the other parameters will be Chris@87: * recomputed. RTK 2005-09-02 */ Chris@87: Chris@87: int has_binomial; /* !=0: following parameters initialized for Chris@87: binomial */ Chris@87: double psave; Chris@87: long nsave; Chris@87: double r; Chris@87: double q; Chris@87: double fm; Chris@87: long m; Chris@87: double p1; Chris@87: double xm; Chris@87: double xl; Chris@87: double xr; Chris@87: double c; Chris@87: double laml; Chris@87: double lamr; Chris@87: double p2; Chris@87: double p3; Chris@87: double p4; Chris@87: Chris@87: } Chris@87: rk_state; Chris@87: Chris@87: typedef enum { Chris@87: RK_NOERR = 0, /* no error */ Chris@87: RK_ENODEV = 1, /* no RK_DEV_RANDOM device */ Chris@87: RK_ERR_MAX = 2 Chris@87: } rk_error; Chris@87: Chris@87: /* error strings */ Chris@87: extern char *rk_strerror[RK_ERR_MAX]; Chris@87: Chris@87: /* Maximum generated random value */ Chris@87: #define RK_MAX 0xFFFFFFFFUL Chris@87: Chris@87: #ifdef __cplusplus Chris@87: extern "C" { Chris@87: #endif Chris@87: Chris@87: /* Chris@87: * Initialize the RNG state using the given seed. Chris@87: */ Chris@87: extern void rk_seed(unsigned long seed, rk_state *state); Chris@87: Chris@87: /* Chris@87: * Initialize the RNG state using a random seed. Chris@87: * Uses /dev/random or, when unavailable, the clock (see randomkit.c). Chris@87: * Returns RK_NOERR when no errors occurs. Chris@87: * Returns RK_ENODEV when the use of RK_DEV_RANDOM failed (for example because Chris@87: * there is no such device). In this case, the RNG was initialized using the Chris@87: * clock. Chris@87: */ Chris@87: extern rk_error rk_randomseed(rk_state *state); Chris@87: Chris@87: /* Chris@87: * Returns a random unsigned long between 0 and RK_MAX inclusive Chris@87: */ Chris@87: extern unsigned long rk_random(rk_state *state); Chris@87: Chris@87: /* Chris@87: * Returns a random long between 0 and LONG_MAX inclusive Chris@87: */ Chris@87: extern long rk_long(rk_state *state); Chris@87: Chris@87: /* Chris@87: * Returns a random unsigned long between 0 and ULONG_MAX inclusive Chris@87: */ Chris@87: extern unsigned long rk_ulong(rk_state *state); Chris@87: Chris@87: /* Chris@87: * Returns a random unsigned long between 0 and max inclusive. Chris@87: */ Chris@87: extern unsigned long rk_interval(unsigned long max, rk_state *state); Chris@87: Chris@87: /* Chris@87: * Returns a random double between 0.0 and 1.0, 1.0 excluded. Chris@87: */ Chris@87: extern double rk_double(rk_state *state); Chris@87: Chris@87: /* Chris@87: * fill the buffer with size random bytes Chris@87: */ Chris@87: extern void rk_fill(void *buffer, size_t size, rk_state *state); Chris@87: Chris@87: /* Chris@87: * fill the buffer with randombytes from the random device Chris@87: * Returns RK_ENODEV if the device is unavailable, or RK_NOERR if it is Chris@87: * On Unix, if strong is defined, RK_DEV_RANDOM is used. If not, RK_DEV_URANDOM Chris@87: * is used instead. This parameter has no effect on Windows. Chris@87: * Warning: on most unixes RK_DEV_RANDOM will wait for enough entropy to answer Chris@87: * which can take a very long time on quiet systems. Chris@87: */ Chris@87: extern rk_error rk_devfill(void *buffer, size_t size, int strong); Chris@87: Chris@87: /* Chris@87: * fill the buffer using rk_devfill if the random device is available and using Chris@87: * rk_fill if is is not Chris@87: * parameters have the same meaning as rk_fill and rk_devfill Chris@87: * Returns RK_ENODEV if the device is unavailable, or RK_NOERR if it is Chris@87: */ Chris@87: extern rk_error rk_altfill(void *buffer, size_t size, int strong, Chris@87: rk_state *state); Chris@87: Chris@87: /* Chris@87: * return a random gaussian deviate with variance unity and zero mean. Chris@87: */ Chris@87: extern double rk_gauss(rk_state *state); Chris@87: Chris@87: #ifdef __cplusplus Chris@87: } Chris@87: #endif Chris@87: Chris@87: #endif /* _RANDOMKIT_ */