summaryrefslogtreecommitdiffstats
path: root/src/core/random.c
diff options
context:
space:
mode:
authorMichael Brown2007-01-19 03:02:59 +0100
committerMichael Brown2007-01-19 03:02:59 +0100
commit4256b3338a414d7f2e63b0be403d362a0782a707 (patch)
tree37832836e5a2df57d5ad8356da68fdddbe680b1a /src/core/random.c
parentvsprintf.h is gPXE-specific; move it to include/gpxe (diff)
downloadipxe-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/random.c')
-rw-r--r--src/core/random.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/core/random.c b/src/core/random.c
new file mode 100644
index 00000000..c15bb6de
--- /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;
+}