source: bootcd/isolinux/syslinux-6.03/gpxe/src/core/random.c

Last change on this file was e16e8f2, checked in by Edwin Eefting <edwin@datux.nl>, 3 years ago

bootstuff

  • Property mode set to 100644
File size: 795 bytes
Line 
1/** @file
2 *
3 * Random number generation
4 *
5 */
6
7FILE_LICENCE ( GPL2_OR_LATER );
8
9#include <stdlib.h>
10#include <gpxe/timer.h>
11
12static int32_t rnd_seed = 0;
13
14/**
15 * Seed the pseudo-random number generator
16 *
17 * @v seed              Seed value
18 */
19void srandom ( unsigned int seed ) {
20        rnd_seed = seed;
21}
22
23/**
24 * Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
25 *
26 * @ret rand            Pseudo-random number
27 */
28long int random ( void ) {
29        int32_t q;
30
31        if ( ! rnd_seed ) /* Initialize linear congruential generator */
32                srandom ( currticks() );
33
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;
41}
Note: See TracBrowser for help on using the repository browser.