Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / third_party / angle / util / random_utils.h
1 //
2 // Copyright (c) 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 // TODO(jmadill): Rework this if Chromium decides to ban <random>
14 #include <random>
15
16 #include <export.h>
17
18 namespace angle
19 {
20
21 class ANGLE_EXPORT RNG
22 {
23   public:
24     // Seed from clock
25     RNG();
26     // Seed from fixed number.
27     RNG(unsigned int seed);
28     ~RNG();
29
30     void reseed(unsigned int newSeed);
31
32     int randomInt();
33     int randomIntBetween(int min, int max);
34     unsigned int randomUInt();
35     float randomFloat();
36     float randomFloatBetween(float min, float max);
37     float randomNegativeOneToOne();
38
39   private:
40     std::default_random_engine mGenerator;
41 };
42
43 // Implemented inline to avoid cross-module allocation issues.
44 inline void FillVectorWithRandomUBytes(RNG *rng, std::vector<uint8_t> *data)
45 {
46     for (size_t i = 0; i < data->size(); ++i)
47     {
48         (*data)[i] = static_cast<uint8_t>(rng->randomIntBetween(0, 255));
49     }
50 }
51
52 inline void FillVectorWithRandomUBytes(std::vector<uint8_t> *data)
53 {
54     RNG rng;
55     FillVectorWithRandomUBytes(&rng, data);
56 }
57
58 }  // namespace angle
59
60 #endif // UTIL_RANDOM_UTILS_H