Initialize Random::Range() 41/154441/1
authorSeoyeon Kim <seoyeon2.kim@samsung.com>
Tue, 10 Oct 2017 08:04:01 +0000 (17:04 +0900)
committerSeoyeon Kim <seoyeon2.kim@samsung.com>
Tue, 10 Oct 2017 08:06:56 +0000 (17:06 +0900)
- For security, the random number generator would be initialized
to use rand().

Change-Id: I6930001c41001ec217bf706d84dc15b71a17d1fe
Signed-off-by: Seoyeon Kim <seoyeon2.kim@samsung.com>
dali/public-api/math/random.h

index 8067112..2e8391e 100644 (file)
@@ -48,6 +48,16 @@ inline float Range(float f0, float f1)
 {
   float min = std::min(f0, f1);
   float max = std::max(f0, f1);
+
+  // Ensure we initialize only once. As it's inlined, this static variable will exist in the code-block using it, thus,
+  // will be created and then initialized again when another code-block uses this.
+  static bool initialized( false );
+  if( !initialized )
+  {
+    srand( time( NULL ) );
+    initialized = true;
+  }
+
   return min + (rand() & 0xfff) * (max-min) * (1.0f/4095.0f);
 }