3 /*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Random number generator utilities.
24 *//*--------------------------------------------------------------------*/
29 #include <iterator> // std::distance()
30 #include <algorithm> // std::swap()
35 //! Random self-test - compare returned values against hard-coded values.
36 void Random_selfTest (void);
41 Random (deUint32 seed) { deRandom_init(&m_rnd, seed); }
44 float getFloat (void) { return deRandom_getFloat(&m_rnd); }
45 double getDouble (void) { return deRandom_getDouble(&m_rnd); }
46 bool getBool (void) { return deRandom_getBool(&m_rnd) == DE_TRUE; }
48 float getFloat (float min, float max);
49 double getDouble (double min, double max);
50 int getInt (int min, int max);
52 deUint64 getUint64 (void) { deUint32 upper = getUint32(); return (deUint64)upper << 32ull | (deUint64)getUint32(); }
53 deUint32 getUint32 (void) { return deRandom_getUint32(&m_rnd); }
54 deUint16 getUint16 (void) { return (deUint16)deRandom_getUint32(&m_rnd); }
55 deUint8 getUint8 (void) { return (deUint8)deRandom_getUint32(&m_rnd); }
57 template <class InputIter, class OutputIter>
58 void choose (InputIter first, InputIter last, OutputIter result, int numItems);
60 template <typename T, class InputIter>
61 T choose (InputIter first, InputIter last);
63 // \note Weights must be floats
64 template <typename T, class InputIter, class WeightIter>
65 T chooseWeighted (InputIter first, InputIter last, WeightIter weight);
67 template <class Iterator>
68 void shuffle (Iterator first, Iterator last);
70 bool operator== (const Random& other) const;
71 bool operator!= (const Random& other) const;
75 } DE_WARN_UNUSED_TYPE;
77 // Inline implementations
79 inline float Random::getFloat (float min, float max)
81 DE_ASSERT(min <= max);
82 return min + (max-min)*getFloat();
85 inline double Random::getDouble (double min, double max)
87 DE_ASSERT(min <= max);
88 return min + (max-min)*getDouble();
91 inline int Random::getInt (int min, int max)
93 DE_ASSERT(min <= max);
94 if (min == (int)0x80000000 && max == (int)0x7fffffff)
95 return (int)getUint32();
97 return min + (int)(getUint32() % (deUint32)(max-min+1));
100 // Template implementations
102 template <class InputIter, class OutputIter>
103 void Random::choose (InputIter first, InputIter last, OutputIter result, int numItems)
105 // Algorithm: Reservoir sampling
106 // http://en.wikipedia.org/wiki/Reservoir_sampling
107 // \note Will not work for suffling an array. Use suffle() instead.
110 for (ndx = 0; first != last; ++first, ++ndx)
113 *(result + ndx) = *first;
116 int r = getInt(0, ndx);
118 *(result + r) = *first;
122 DE_ASSERT(ndx >= numItems);
125 template <typename T, class InputIter>
126 T Random::choose (InputIter first, InputIter last)
129 DE_ASSERT(first != last);
130 choose(first, last, &val, 1);
134 template <typename T, class InputIter, class WeightIter>
135 T Random::chooseWeighted (InputIter first, InputIter last, WeightIter weight)
137 // Compute weight sum
138 float weightSum = 0.0f;
140 for (ndx = 0; (first + ndx) != last; ndx++)
141 weightSum += *(weight + ndx);
143 // Random point in 0..weightSum
144 float p = getFloat(0.0f, weightSum);
146 // Find item in range
147 InputIter lastNonZero = last;
148 float curWeight = 0.0f;
149 for (ndx = 0; (first + ndx) != last; ndx++)
151 float w = *(weight + ndx);
156 return *(first + ndx);
158 lastNonZero = first + ndx;
161 DE_ASSERT(lastNonZero != last);
165 template <class Iterator>
166 void Random::shuffle (Iterator first, Iterator last)
170 // Fisher-Yates suffle
171 int numItems = (int)std::distance(first, last);
173 for (int i = numItems-1; i >= 1; i--)
175 int j = getInt(0, i);
176 swap(*(first + i), *(first + j));
182 #endif // _DERANDOM_HPP