1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // random_utils:
7*8975f5c5SAndroid Build Coastguard Worker // Helper functions for random number generation.
8*8975f5c5SAndroid Build Coastguard Worker //
9*8975f5c5SAndroid Build Coastguard Worker
10*8975f5c5SAndroid Build Coastguard Worker #ifndef UTIL_RANDOM_UTILS_H
11*8975f5c5SAndroid Build Coastguard Worker #define UTIL_RANDOM_UTILS_H
12*8975f5c5SAndroid Build Coastguard Worker
13*8975f5c5SAndroid Build Coastguard Worker #include <random>
14*8975f5c5SAndroid Build Coastguard Worker #include <vector>
15*8975f5c5SAndroid Build Coastguard Worker
16*8975f5c5SAndroid Build Coastguard Worker #include "common/vector_utils.h"
17*8975f5c5SAndroid Build Coastguard Worker #include "util/util_export.h"
18*8975f5c5SAndroid Build Coastguard Worker
19*8975f5c5SAndroid Build Coastguard Worker namespace angle
20*8975f5c5SAndroid Build Coastguard Worker {
21*8975f5c5SAndroid Build Coastguard Worker
22*8975f5c5SAndroid Build Coastguard Worker class ANGLE_UTIL_EXPORT RNG
23*8975f5c5SAndroid Build Coastguard Worker {
24*8975f5c5SAndroid Build Coastguard Worker public:
25*8975f5c5SAndroid Build Coastguard Worker // Seed from clock
26*8975f5c5SAndroid Build Coastguard Worker RNG();
27*8975f5c5SAndroid Build Coastguard Worker // Seed from fixed number.
28*8975f5c5SAndroid Build Coastguard Worker RNG(unsigned int seed);
29*8975f5c5SAndroid Build Coastguard Worker ~RNG();
30*8975f5c5SAndroid Build Coastguard Worker
31*8975f5c5SAndroid Build Coastguard Worker void reseed(unsigned int newSeed);
32*8975f5c5SAndroid Build Coastguard Worker
33*8975f5c5SAndroid Build Coastguard Worker bool randomBool(float probTrue = 0.5f);
34*8975f5c5SAndroid Build Coastguard Worker int randomInt();
35*8975f5c5SAndroid Build Coastguard Worker int randomIntBetween(int min, int max);
36*8975f5c5SAndroid Build Coastguard Worker unsigned int randomUInt();
37*8975f5c5SAndroid Build Coastguard Worker float randomFloat();
38*8975f5c5SAndroid Build Coastguard Worker float randomFloatBetween(float min, float max);
39*8975f5c5SAndroid Build Coastguard Worker float randomFloatNonnegative();
40*8975f5c5SAndroid Build Coastguard Worker float randomNegativeOneToOne();
41*8975f5c5SAndroid Build Coastguard Worker
42*8975f5c5SAndroid Build Coastguard Worker template <class T>
randomSelect(std::vector<T> & elements)43*8975f5c5SAndroid Build Coastguard Worker T &randomSelect(std::vector<T> &elements)
44*8975f5c5SAndroid Build Coastguard Worker {
45*8975f5c5SAndroid Build Coastguard Worker return elements[randomIntBetween(0, static_cast<int>(elements.size()) - 1)];
46*8975f5c5SAndroid Build Coastguard Worker }
47*8975f5c5SAndroid Build Coastguard Worker
48*8975f5c5SAndroid Build Coastguard Worker template <class T>
randomSelect(const std::vector<T> & elements)49*8975f5c5SAndroid Build Coastguard Worker const T &randomSelect(const std::vector<T> &elements)
50*8975f5c5SAndroid Build Coastguard Worker {
51*8975f5c5SAndroid Build Coastguard Worker return elements.at(randomIntBetween(0, static_cast<int>(elements.size()) - 1));
52*8975f5c5SAndroid Build Coastguard Worker }
53*8975f5c5SAndroid Build Coastguard Worker
54*8975f5c5SAndroid Build Coastguard Worker private:
55*8975f5c5SAndroid Build Coastguard Worker std::default_random_engine mGenerator;
56*8975f5c5SAndroid Build Coastguard Worker };
57*8975f5c5SAndroid Build Coastguard Worker
58*8975f5c5SAndroid Build Coastguard Worker // Implemented inline to avoid cross-module allocation issues.
FillVectorWithRandomUBytes(RNG * rng,std::vector<uint8_t> * data)59*8975f5c5SAndroid Build Coastguard Worker inline void FillVectorWithRandomUBytes(RNG *rng, std::vector<uint8_t> *data)
60*8975f5c5SAndroid Build Coastguard Worker {
61*8975f5c5SAndroid Build Coastguard Worker for (size_t i = 0; i < data->size(); ++i)
62*8975f5c5SAndroid Build Coastguard Worker {
63*8975f5c5SAndroid Build Coastguard Worker (*data)[i] = static_cast<uint8_t>(rng->randomIntBetween(0, 255));
64*8975f5c5SAndroid Build Coastguard Worker }
65*8975f5c5SAndroid Build Coastguard Worker }
66*8975f5c5SAndroid Build Coastguard Worker
FillVectorWithRandomUBytes(std::vector<uint8_t> * data)67*8975f5c5SAndroid Build Coastguard Worker inline void FillVectorWithRandomUBytes(std::vector<uint8_t> *data)
68*8975f5c5SAndroid Build Coastguard Worker {
69*8975f5c5SAndroid Build Coastguard Worker RNG rng;
70*8975f5c5SAndroid Build Coastguard Worker FillVectorWithRandomUBytes(&rng, data);
71*8975f5c5SAndroid Build Coastguard Worker }
72*8975f5c5SAndroid Build Coastguard Worker
RandomVec3(int seed,float minValue,float maxValue)73*8975f5c5SAndroid Build Coastguard Worker inline Vector3 RandomVec3(int seed, float minValue, float maxValue)
74*8975f5c5SAndroid Build Coastguard Worker {
75*8975f5c5SAndroid Build Coastguard Worker RNG rng(seed);
76*8975f5c5SAndroid Build Coastguard Worker srand(seed);
77*8975f5c5SAndroid Build Coastguard Worker return Vector3(rng.randomFloatBetween(minValue, maxValue),
78*8975f5c5SAndroid Build Coastguard Worker rng.randomFloatBetween(minValue, maxValue),
79*8975f5c5SAndroid Build Coastguard Worker rng.randomFloatBetween(minValue, maxValue));
80*8975f5c5SAndroid Build Coastguard Worker }
81*8975f5c5SAndroid Build Coastguard Worker
RandomVec4(int seed,float minValue,float maxValue)82*8975f5c5SAndroid Build Coastguard Worker inline Vector4 RandomVec4(int seed, float minValue, float maxValue)
83*8975f5c5SAndroid Build Coastguard Worker {
84*8975f5c5SAndroid Build Coastguard Worker RNG rng(seed);
85*8975f5c5SAndroid Build Coastguard Worker srand(seed);
86*8975f5c5SAndroid Build Coastguard Worker return Vector4(
87*8975f5c5SAndroid Build Coastguard Worker rng.randomFloatBetween(minValue, maxValue), rng.randomFloatBetween(minValue, maxValue),
88*8975f5c5SAndroid Build Coastguard Worker rng.randomFloatBetween(minValue, maxValue), rng.randomFloatBetween(minValue, maxValue));
89*8975f5c5SAndroid Build Coastguard Worker }
90*8975f5c5SAndroid Build Coastguard Worker } // namespace angle
91*8975f5c5SAndroid Build Coastguard Worker
92*8975f5c5SAndroid Build Coastguard Worker #endif // UTIL_RANDOM_UTILS_H
93