Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / third_party / angle / util / random_utils.cpp
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 #include "random_utils.h"
11
12 #include <chrono>
13
14 #include <cstdlib>
15
16 namespace angle
17 {
18
19 // Seed from clock
20 RNG::RNG()
21 {
22     long long timeSeed = std::chrono::system_clock::now().time_since_epoch().count();
23     mGenerator.seed(static_cast<unsigned int>(timeSeed));
24 }
25
26 // Seed from fixed number.
27 RNG::RNG(unsigned int seed) : mGenerator(seed)
28 {
29 }
30
31 RNG::~RNG()
32 {
33 }
34
35 void RNG::reseed(unsigned int newSeed)
36 {
37     mGenerator.seed(newSeed);
38 }
39
40 int RNG::randomInt()
41 {
42     std::uniform_int_distribution<int> intDistribution;
43     return intDistribution(mGenerator);
44 }
45
46 int RNG::randomIntBetween(int min, int max)
47 {
48     std::uniform_int_distribution<int> intDistribution(min, max);
49     return intDistribution(mGenerator);
50 }
51
52 unsigned int RNG::randomUInt()
53 {
54     std::uniform_int_distribution<unsigned int> uintDistribution;
55     return uintDistribution(mGenerator);
56 }
57
58 float RNG::randomFloat()
59 {
60     std::uniform_real_distribution<float> floatDistribution;
61     return floatDistribution(mGenerator);
62 }
63
64 float RNG::randomFloatBetween(float min, float max)
65 {
66     std::uniform_real_distribution<float> floatDistribution(min, max);
67     return floatDistribution(mGenerator);
68 }
69
70 float RNG::randomNegativeOneToOne()
71 {
72     return randomFloatBetween(-1.0f, 1.0f);
73 }
74
75 }  // namespace angle