Hello everybody,

lately I've been working on a multiscreen setup driven by multiple host machines. In order to get the same random scenery objects, all machines need to use the same random seed. Up to now, the random seed is taken from system time, which leads to inconsistencies.

The attached patches add a command line option --random-seed=<int>, which allows to explicitly specify a (positive) seed, or -1 (the default when the option is not given) to seed from system time, or -2 to seed from system time in 10 minutes intervals.

This is however only a first step, there are remaining inconsistencies, most prominently 3D clouds differ, even between two runs on the same machine. I suspect this is because there is only one instance of the random number generator, which may be accessed in non-deterministic order from different threads. The solution is probably to make a random number stream class, of which each relevant subsystem/class/whatever keeps its own static instance. I'll be looking further into this, but maybe someone has some experience on this to share?

Best regards,
        Andreas
commit a217c57fc723ce88397295cfda0eff2eb9071ad3
Author: Andreas Gaeb <a.g...@web.de>
Date:   Wed Sep 7 20:14:54 2011 +0200

    added command line option to specify random seed

diff --git a/scripts/completion/fg-completion.bash b/scripts/completion/fg-completion.bash
index 4f34e2d..464fefa 100644
--- a/scripts/completion/fg-completion.bash
+++ b/scripts/completion/fg-completion.bash
@@ -28,6 +28,7 @@ __fgfs_options="
 	--enable-intro-music
 	--units-feet
 	--units-meters
+	--random-seed=
 	--disable-sound
 	--enable-sound
 	--disable-panel
diff --git a/scripts/completion/fg-completion.zsh b/scripts/completion/fg-completion.zsh
index f4cda35..08309e0 100644
--- a/scripts/completion/fg-completion.zsh
+++ b/scripts/completion/fg-completion.zsh
@@ -103,6 +103,7 @@ _fgfs_options=(
 	'--aero=[Select aircraft aerodynamics model to load]' \
 	'--model-hz=[Run the FDM this rate (iterations per second)]' \
 	'--speed=[Run the FDM n times faster than real time]' \
+	'--random-seed=[Specify value for random seed]' \
 	'--aircraft-dir=[Aircraft directory relative to the path of the executable]:Aircraft directory:_directories' \
 	'--timeofday=[Specify a time of day]:Time of day:(real dawn morning noon afternoon dusk evening midnight)' \
 	'--time-offset=[Add this time offset (+/-hh:mm:ss)]' \
diff --git a/src/Main/main.cxx b/src/Main/main.cxx
index 6fa94dc..f457204 100644
--- a/src/Main/main.cxx
+++ b/src/Main/main.cxx
@@ -595,9 +595,6 @@ int fgMainInit( int argc, char **argv ) {

     globals = new FGGlobals;

-    // seed the random number generator
-    sg_srandom_time();
-
     FGControls *controls = new FGControls;
     globals->set_controls( controls );

@@ -642,6 +639,19 @@ int fgMainInit( int argc, char **argv ) {
         exit(-1);
     }

+    // seed the random number generator
+    int seed = fgGetInt("/sim/random-seed", -1);
+    if (seed < -2) {
+      SG_LOG( SG_GENERAL, SG_ALERT, "Random seed must be >= -2. Will use system time as random seed.");
+      sg_srandom_time();
+    } else if (seed == -2) {
+      sg_srandom_time_10();
+    } else if (seed == -1) {
+      sg_srandom_time();
+    } else {
+      sg_srandom(seed);
+    }
+
     // Initialize the Window/Graphics environment.
     fgOSInit(&argc, argv);
     _bootstrap_OSInit++;
diff --git a/src/Main/options.cxx b/src/Main/options.cxx
index 172d133..7bb0601 100644
--- a/src/Main/options.cxx
+++ b/src/Main/options.cxx
@@ -1374,6 +1374,7 @@ struct OptionDesc {
     {"aircraft-dir",                 true,  OPTION_STRING, "/sim/aircraft-dir", false, "", 0 },
     {"model-hz",                     true,  OPTION_INT,    "/sim/model-hz", false, "", 0 },
     {"speed",                        true,  OPTION_INT,    "/sim/speed-up", false, "", 0 },
+    {"random-seed",                  true,  OPTION_INT,    "/sim/random-seed", false, "", 0 },
     {"trim",                         false, OPTION_BOOL,   "/sim/presets/trim", true, "", 0 },
     {"notrim",                       false, OPTION_BOOL,   "/sim/presets/trim", false, "", 0 },
     {"on-ground",                    false, OPTION_BOOL,   "/sim/presets/onground", true, "", 0 },
commit 9c84f590ecef747714d598635938f2e22a441a84
Author: Andreas Gaeb <a.g...@web.de>
Date:   Fri Sep 9 10:33:58 2011 +0200

    --random-seed option added to help

diff --git a/Translations/strings-default.xml b/Translations/strings-default.xml
index e689a26..3f6f339 100644
--- a/Translations/strings-default.xml
+++ b/Translations/strings-default.xml
@@ -116,6 +116,7 @@
  <config-desc>Load additional properties from path</config-desc>
  <units-feet-desc>Use feet for distances</units-feet-desc>
  <units-meters-desc>Use meters for distances</units-meters-desc>
+ <random-seed-desc>Use value as repeatable random seed. Value=-1 seeds from system time, value=-2 from system time in 10 minute intervals</random-seed-desc>
 
  <!-- Features options -->
  <environment-options>Environment Options</environment-options>
diff --git a/options.xml b/options.xml
index 293cfd6..37582cc 100644
--- a/options.xml
+++ b/options.xml
@@ -145,6 +145,12 @@
     <description>strings/units-meters-desc</description>
    </option>
 
+   <option>
+    <name>random-seed</name>
+    <arg>value</arg>
+    <description>strings/random-seed-desc</description>
+   </option>
+
   </section>
 
   <section>
------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the 
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to