iPXE
random.c
Go to the documentation of this file.
1 /** @file
2  *
3  * Random number generation
4  *
5  */
6 
7 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
8 FILE_SECBOOT ( PERMITTED );
9 
10 #include <stddef.h>
11 #include <stdlib.h>
12 #include <time.h>
13 
14 static int32_t rnd_seed = 0;
15 
16 /**
17  * Seed the pseudo-random number generator
18  *
19  * @v seed Seed value
20  */
21 void srandom ( unsigned int seed ) {
22  rnd_seed = seed;
23  if ( ! rnd_seed )
24  rnd_seed = 4; /* Chosen by fair dice roll */
25 }
26 
27 /**
28  * Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
29  *
30  * @ret rand Pseudo-random number
31  */
32 long int random ( void ) {
33  int32_t q;
34 
35  /* Initialize linear congruential generator */
36  if ( ! rnd_seed )
37  srandom ( time ( NULL ) );
38 
39  /* simplified version of the LCG given in Bruce Schneier's
40  "Applied Cryptography" */
41  q = ( rnd_seed / 53668 );
42  rnd_seed = ( 40014 * ( rnd_seed - 53668 * q ) - 12211 * q );
43  if ( rnd_seed < 0 )
44  rnd_seed += 2147483563L;
45  return rnd_seed;
46 }
FILE_SECBOOT(PERMITTED)
static int32_t rnd_seed
Definition: random.c:14
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
void srandom(unsigned int seed)
Seed the pseudo-random number generator.
Definition: random.c:21
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:32
Date and time.
signed int int32_t
Definition: stdint.h:17
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322