diff options
| author | Michael Brown | 2007-01-19 03:02:59 +0100 |
|---|---|---|
| committer | Michael Brown | 2007-01-19 03:02:59 +0100 |
| commit | 4256b3338a414d7f2e63b0be403d362a0782a707 (patch) | |
| tree | 37832836e5a2df57d5ad8356da68fdddbe680b1a /src/core | |
| parent | vsprintf.h is gPXE-specific; move it to include/gpxe (diff) | |
| download | ipxe-4256b3338a414d7f2e63b0be403d362a0782a707.tar.gz ipxe-4256b3338a414d7f2e63b0be403d362a0782a707.tar.xz ipxe-4256b3338a414d7f2e63b0be403d362a0782a707.zip | |
Split random number generation out into core/random.c, and create the
correct prototypes for srandom(), rand() and srand().
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/misc.c | 18 | ||||
| -rw-r--r-- | src/core/random.c | 38 |
2 files changed, 38 insertions, 18 deletions
diff --git a/src/core/misc.c b/src/core/misc.c index e150709a1..e214a6284 100644 --- a/src/core/misc.c +++ b/src/core/misc.c @@ -55,24 +55,6 @@ uint16_t add_ipchksums(unsigned long offset, uint16_t sum, uint16_t new) return (~checksum) & 0xFFFF; } - - -/************************************************************************** -RANDOM - compute a random number between 0 and 2147483647L or 2147483562? -**************************************************************************/ -long int random(void) -{ - static int32_t seed = 0; - int32_t q; - if (!seed) /* Initialize linear congruential generator */ - seed = currticks(); - /* simplified version of the LCG given in Bruce Schneier's - "Applied Cryptography" */ - q = seed/53668; - if ((seed = 40014*(seed-53668*q) - 12211*q) < 0) seed += 2147483563L; - return seed; -} - /************************************************************************** SLEEP **************************************************************************/ diff --git a/src/core/random.c b/src/core/random.c new file mode 100644 index 000000000..c15bb6de3 --- /dev/null +++ b/src/core/random.c @@ -0,0 +1,38 @@ +/** @file + * + * Random number generation + * + */ + +#include <stdlib.h> + +static int32_t rnd_seed = 0; + +/** + * Seed the pseudo-random number generator + * + * @v seed Seed value + */ +void srandom ( unsigned int seed ) { + rnd_seed = seed; +} + +/** + * Generate a pseudo-random number between 0 and 2147483647L or 2147483562? + * + * @ret rand Pseudo-random number + */ +long int random ( void ) { + int32_t q; + + if ( ! rnd_seed ) /* Initialize linear congruential generator */ + srandom ( currticks() ); + + /* simplified version of the LCG given in Bruce Schneier's + "Applied Cryptography" */ + q = ( rnd_seed / 53668 ); + rnd_seed = ( 40014 * ( rnd_seed - 53668 * q ) - 12211 * q ); + if ( rnd_seed < 0 ) + rnd_seed += 2147483563L; + return rnd_seed; +} |
