projects
/
pintos-anon
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Allow user to provide random seed to random_init().
[pintos-anon]
/
src
/
lib
/
random.c
diff --git
a/src/lib/random.c
b/src/lib/random.c
index 39452a99666246ae33ccd96da58eaf62aefe7b97..d4ffa6df216d0b43c56101ea85839020512e4d61 100644
(file)
--- a/
src/lib/random.c
+++ b/
src/lib/random.c
@@
-1,9
+1,11
@@
#include "random.h"
#include "random.h"
+#include <stdbool.h>
#include <stdint.h>
/* RC4-based pseudo-random state. */
static uint8_t s[256];
static uint8_t s_i, s_j;
#include <stdint.h>
/* 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)
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;
}
*b = t;
}
-static uint8_t
-key_byte (int idx)
-{
- return idx ^ 0xff;
-}
-
void
void
-random_init (
voi
d)
+random_init (
unsigned see
d)
{
{
+ uint8_t *seedp = (uint8_t *) &seed;
int i;
uint8_t j;
int i;
uint8_t j;
+ if (inited)
+ return;
+
for (i = 0; i < 256; i++)
s[i] = i;
for (i = j = 0; i < 256; i++)
{
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;
swap_byte (s + i, s + j);
}
s_i = s_j = 0;
+ inited = true;
}
void
}
void