[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / core / random.c
blob6e7374e349868cee343409650a755ad5474deb44
1 /** @file
3 * Random number generation
5 */
7 FILE_LICENCE ( GPL2_OR_LATER );
9 #include <stdlib.h>
10 #include <gpxe/timer.h>
12 static int32_t rnd_seed = 0;
14 /**
15 * Seed the pseudo-random number generator
17 * @v seed Seed value
19 void srandom ( unsigned int seed ) {
20 rnd_seed = seed;
23 /**
24 * Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
26 * @ret rand Pseudo-random number
28 long int random ( void ) {
29 int32_t q;
31 if ( ! rnd_seed ) /* Initialize linear congruential generator */
32 srandom ( currticks() );
34 /* simplified version of the LCG given in Bruce Schneier's
35 "Applied Cryptography" */
36 q = ( rnd_seed / 53668 );
37 rnd_seed = ( 40014 * ( rnd_seed - 53668 * q ) - 12211 * q );
38 if ( rnd_seed < 0 )
39 rnd_seed += 2147483563L;
40 return rnd_seed;