[Tizen] Add codes for Dali Windows Backend
[platform/core/uifw/dali-core.git] / dali / public-api / math / random.h
old mode 100644 (file)
new mode 100755 (executable)
index 394b2dd..bc6fcbd
@@ -2,7 +2,7 @@
 #define __DALI_RANDOM_H__
 
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
  * limitations under the License.
  *
  */
+// EXTERNAL INCLUDES
+#include <ctime>
 
 // INTERNAL INCLUDES
 #include <dali/public-api/math/vector4.h>
@@ -35,11 +37,6 @@ namespace Dali
 namespace Random
 {
 
-namespace
-{
-  __thread unsigned int seed;
-}
-
 /**
  * @brief Returns a random number between f0 and f1.
  *
@@ -47,21 +44,32 @@ namespace
  * @SINCE_1_0.0
  * @param[in] f0 The lower bound
  * @param[in] f1 The upper bound
- * @return a random value between the lower and upper bound
+ * @return A random value between the lower and upper bound
  */
 inline float Range(float f0, float f1)
 {
-  seed = time(NULL);
   float min = std::min(f0, f1);
   float max = std::max(f0, f1);
-  return min + (rand_r(&seed) & 0xfff) * (max-min) * (1.0f/4095.0f);
+
+  // 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 )
+  {
+    auto seed = time( NULL );
+    srand( seed );
+    initialized = true;
+  }
+
+  auto randValue = rand();
+  return (randValue & 0xfff) * (1.0f/4095.0f) * (max-min) + min;
 }
 
 /**
  * @brief Function to return a normalized axis in a random direction.
  *
  * @SINCE_1_0.0
- * @return the axis
+ * @return The axis
  */
 inline Vector4 Axis()
 {