License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / capi / dali / public-api / math / random.h
1 #ifndef __DALI_RANDOM_H__
2 #define __DALI_RANDOM_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 /**
22  * @addtogroup CAPI_DALI_MATH_MODULE
23  * @{
24  */
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/math/vector4.h>
28
29 namespace Dali DALI_IMPORT_API
30 {
31
32 /**
33  * @brief Provides methods to generate and use random values.
34  */
35 namespace Random
36 {
37
38 /**
39  * @brief Returns a random number between f0 and f1.
40  *
41  * Note, uses a limited number of values.
42  * @param[in] f0 the lower bound
43  * @param[in] f1 the upper bound
44  * @return a random value between the lower and upper bound
45  */
46 inline float Range(float f0, float f1)
47 {
48   float min = std::min(f0, f1);
49   float max = std::max(f0, f1);
50   return min + (rand() & 0xfff) * (max-min) * (1.0f/4095.0f);
51 }
52
53 /**
54  * @brief Function to return a normalized axis in a random direction.
55  *
56  * @return the axis
57  */
58 inline Vector4 Axis()
59 {
60   Vector4 axis;
61   // This function needs to return a vector with direction, that is to say a non-zero vector
62   // There is a possibility that a random vector will be a zero vector, so a loop is needed to ensure that a non-zero vector is returned
63   do
64   {
65     axis.x = Range(-1.0f, 1.0f);
66     axis.y = Range( 0.0f, 1.0f);
67     axis.z = Range( 0.0f, 1.0f);
68     axis.w = 0.0f;
69   } while (axis == Vector4::ZERO);
70   axis.Normalize();
71   return axis;
72 }
73
74 /**
75  * @brief Returns true if the value given is greater than a random value between 0 and 1.
76  *
77  * @param chance  A value between 0 and 1. [Default: 0.5]
78  * @return        true if chance greater than the random value, otherwise false.
79  */
80 inline bool Chance(float chance = 0.5f)
81 {
82   return chance > Range(0.0f, 1.0f);
83 }
84
85 } // namespace Random
86
87 } // namespace Dali
88
89 /**
90  * @}
91  */
92 #endif // __DALI_RANDOM_H__