Fix %' grouping in printf().
[pintos-anon] / src / lib / random.c
index 39452a99666246ae33ccd96da58eaf62aefe7b97..63d798d6bf6394dcb017f096c5980cf4722c4fb9 100644 (file)
@@ -1,10 +1,26 @@
 #include "random.h"
+#include <stdbool.h>
 #include <stdint.h>
+#include "debug.h"
 
-/* RC4-based pseudo-random state. */
-static uint8_t s[256];
-static uint8_t s_i, s_j;
+/* RC4-based pseudo-random number generator (PRNG).
 
+   RC4 is a stream cipher.  We're not using it here for its
+   cryptographic properties, but because it is easy to implement
+   and its output is plenty random for non-cryptographic
+   purposes.
+
+   See http://en.wikipedia.org/wiki/RC4_(cipher) for information
+   on RC4.*/
+
+/* RC4 state. */
+static uint8_t s[256];          /* S[]. */
+static uint8_t s_i, s_j;        /* i, j. */
+
+/* Already initialized? */
+static bool inited;     
+
+/* Swaps the bytes pointed to by A and B. */
 static inline void
 swap_byte (uint8_t *a, uint8_t *b) 
 {
@@ -13,34 +29,37 @@ swap_byte (uint8_t *a, uint8_t *b)
   *b = t;
 }
 
-static uint8_t
-key_byte (int idx) 
-{
-  return idx ^ 0xff;
-}
-
+/* Initializes the PRNG with the given SEED.
+   Does nothing if the PRNG has already been initialized. */
 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;
 }
 
+/* Writes SIZE random bytes into BUF. */
 void
 random_bytes (void *buf_, size_t size) 
 {
   uint8_t *buf;
 
+  ASSERT (inited);
   for (buf = buf_; size-- > 0; buf++)
     {
       uint8_t s_k;