Reviewers: Erik Corry, sandholm,

Message:
Small review. For use in running "benchmarks" that depend on randomness in a
deterministic way.

Description:
Added flag for seeding the random generator deterministically.

Please review this at http://codereview.chromium.org/1247003

Affected files:
  M src/flag-definitions.h
  M src/v8.cc


Index: src/flag-definitions.h
diff --git a/src/flag-definitions.h b/src/flag-definitions.h
index 6d0b9580c0494fad5924a776094ec364c81ad13e..69acc06df38b8861fd5d7eaa5122a5d2151152a9 100644
--- a/src/flag-definitions.h
+++ b/src/flag-definitions.h
@@ -205,6 +205,9 @@ DEFINE_bool(cleanup_ics_at_gc, true,
             "Flush inline caches prior to mark compact collection.")
 DEFINE_bool(cleanup_caches_in_maps_at_gc, true,
             "Flush code caches in maps during mark compact cycle.")
+DEFINE_int(random_seed, 0,
+           "Default seed for initializing random generator "
+           "(0, the default, means to use system random).")

 DEFINE_bool(canonicalize_object_literal_maps, true,
             "Canonicalize maps for object literals.")
Index: src/v8.cc
diff --git a/src/v8.cc b/src/v8.cc
index 395401d91b2c575ccfe1ca009e403eb3d7b5cc6f..5af200348b4058e4c65075dc4d58102393f63fd9 100644
--- a/src/v8.cc
+++ b/src/v8.cc
@@ -155,6 +155,14 @@ void V8::TearDown() {
 }


+static uint32_t random_seed() {
+  if (FLAG_random_seed == 0) {
+    return random();
+  }
+  return FLAG_random_seed;
+}
+
+
 uint32_t V8::Random() {
   // Random number generator using George Marsaglia's MWC algorithm.
   static uint32_t hi = 0;
@@ -164,8 +172,8 @@ uint32_t V8::Random() {
   // should ever become zero again, or if random() returns zero, we
   // avoid getting stuck with zero bits in hi or lo by re-initializing
   // them on demand.
-  if (hi == 0) hi = random();
-  if (lo == 0) lo = random();
+  if (hi == 0) hi = random_seed();
+  if (lo == 0) lo = random_seed();

   // Mix the bits.
   hi = 36969 * (hi & 0xFFFF) + (hi >> 16);


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

To unsubscribe from this group, send email to v8-dev+unsubscribegooglegroups.com or reply 
to this email with the words "REMOVE ME" as the subject.

Reply via email to