From 3a234a278601791d3ca3dbf58cf78096846fd0b5 Mon Sep 17 00:00:00 2001
From: Jonathan Guinet <jonathan.guinet@c-s.fr>
Date: Fri, 13 Apr 2012 17:54:46 +0200
Subject: [PATCH] ENH: AddRANDParameter option in order to Set mersenne twister
 seed. if no value given rand gen is initialized with time dependant value.

---
 .../otbWrapperApplication.cxx                 | 46 ++++++++++++++++++-
 .../ApplicationEngine/otbWrapperApplication.h |  9 ++++
 .../otbWrapperCommandLineLauncher.cxx         |  6 +--
 3 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/Code/ApplicationEngine/otbWrapperApplication.cxx b/Code/ApplicationEngine/otbWrapperApplication.cxx
index a44992ed0f..95bf39308d 100644
--- a/Code/ApplicationEngine/otbWrapperApplication.cxx
+++ b/Code/ApplicationEngine/otbWrapperApplication.cxx
@@ -123,7 +123,30 @@ void Application::UpdateParameters()
 int Application::Execute()
 {
   int ret = 0;
-  
+  // before execute we set the seed of mersenne twister
+  std::vector<std::string> paramList = GetParametersKeys(true);
+  bool UseSpecificSeed = false;
+
+  for (std::vector<std::string>::const_iterator it = paramList.begin(); it != paramList.end(); ++it)
+    {
+    std::string key = *it;
+
+    if ((key.compare(0, 4, "rand") == 0) && HasValue("rand"))
+      {
+      UseSpecificSeed = true;
+      Parameter* param = GetParameterByKey(key);
+      IntParameter* randParam = dynamic_cast<IntParameter*> (param);
+      int seed = randParam->GetValue();
+      itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->SetSeed(seed);
+      }
+
+    }
+
+  if (!UseSpecificSeed)
+    {
+    itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->Initialize();
+    }
+
   this->DoExecute();
 
   return ret;
@@ -1144,6 +1167,27 @@ void Application::AddRAMParameter(std::string paramKey)
   SetParameterDescription(paramKey, "Available memory for processing (in MB)");
 }
 
+void Application::AddRANDParameter(std::string paramKey, std::string paramName, unsigned int defaultValue)
+{
+  GetParameterList()->AddParameter(ParameterType_Int, paramKey, paramName);
+  SetDefaultParameterInt(paramKey, defaultValue);
+  MandatoryOff(paramKey);
+}
+
+// paramKey default value = rand
+void Application::AddRANDParameter(std::string paramKey)
+{
+  // Get the  RAND Parameter from the configuration file
+
+  GetParameterList()->AddParameter(ParameterType_Int, paramKey, "set user defined seed");
+  MandatoryOff(paramKey);
+  SetParameterDescription(paramKey, "Set specific seed. with integer value.");
+
+}
+
+
+
+
 std::vector< std::pair<std::string, std::string> >
 Application::GetOutputParametersSumUp()
 {
diff --git a/Code/ApplicationEngine/otbWrapperApplication.h b/Code/ApplicationEngine/otbWrapperApplication.h
index 5ae896f609..c4d6564404 100644
--- a/Code/ApplicationEngine/otbWrapperApplication.h
+++ b/Code/ApplicationEngine/otbWrapperApplication.h
@@ -35,6 +35,7 @@
 #include "otbWrapperComplexOutputImageParameter.h"
 
 #include "otbWrapperDocExampleStructure.h"
+#include "itkMersenneTwisterRandomVariateGenerator.h"
 
 namespace otb
 {
@@ -617,6 +618,14 @@ protected:
   /** Add a parameterRAM method with no parameter*/
   void AddRAMParameter(std::string paramKey="ram");
 
+  /** Add a parameterRAND method with parameter
+   * by default seed initialization is based on time value*/
+   void AddRANDParameter(std::string paramKey, std::string paramName, unsigned int defaultValue);
+
+   /** Add a parameterRAND method with no parameter*/
+   void AddRANDParameter(std::string paramKey="rand");
+
+
   /** Remove the items added to the ListWidget */
   void ClearChoices(std::string key);
 
diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
index 077f542d94..f64ac6ddf0 100644
--- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
+++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx
@@ -41,7 +41,7 @@
 #include "otbWrapperStringListParameter.h"
 
 #include "otbStreamingImageFileWriter.h"
-#include "itkMersenneTwisterRandomVariateGenerator.h"
+
 
 #include "otbWrapperApplicationRegistry.h"
 #include "otbWrapperApplication.h"
@@ -645,8 +645,8 @@ void CommandLineLauncher::DisplayHelp()
 void CommandLineLauncher::LoadTestEnv()
 {
   //Set seed for rand and itk mersenne twister
-  srand(1);
-  itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->SetSeed(121212);
+  //srand(1);
+ // itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->SetSeed(121212);
 }
 
 
-- 
GitLab