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