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