From: Ben Pfaff Date: Tue, 31 Aug 2004 22:04:08 +0000 (+0000) Subject: Allow user to provide random seed to random_init(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf2f40d622b3d18afaf03142dc234de3b6a40c81;p=pintos-anon Allow user to provide random seed to random_init(). --- diff --git a/src/lib/random.c b/src/lib/random.c index 39452a9..d4ffa6d 100644 --- a/src/lib/random.c +++ b/src/lib/random.c @@ -1,9 +1,11 @@ #include "random.h" +#include #include /* RC4-based pseudo-random state. */ static uint8_t s[256]; static uint8_t s_i, s_j; +static bool inited; static inline void swap_byte (uint8_t *a, uint8_t *b) @@ -13,27 +15,26 @@ swap_byte (uint8_t *a, uint8_t *b) *b = t; } -static uint8_t -key_byte (int idx) -{ - return idx ^ 0xff; -} - void -random_init (void) +random_init (unsigned seed) { + uint8_t *seedp = (uint8_t *) &seed; int i; uint8_t j; + if (inited) + return; + for (i = 0; i < 256; i++) s[i] = i; for (i = j = 0; i < 256; i++) { - j += s[i] + key_byte (i); + j += s[i] + seedp[i % sizeof seed]; swap_byte (s + i, s + j); } s_i = s_j = 0; + inited = true; } void diff --git a/src/lib/random.h b/src/lib/random.h index d2f3706..cc6eed4 100644 --- a/src/lib/random.h +++ b/src/lib/random.h @@ -3,7 +3,7 @@ #include -void random_init (void); +void random_init (unsigned seed); void random_bytes (void *, size_t); unsigned long random_ulong (void);