xref: /aosp_15_r20/external/angle/util/random_utils.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 #include "random_utils.h"
11*8975f5c5SAndroid Build Coastguard Worker 
12*8975f5c5SAndroid Build Coastguard Worker #include <chrono>
13*8975f5c5SAndroid Build Coastguard Worker 
14*8975f5c5SAndroid Build Coastguard Worker #include <cstdlib>
15*8975f5c5SAndroid Build Coastguard Worker 
16*8975f5c5SAndroid Build Coastguard Worker namespace angle
17*8975f5c5SAndroid Build Coastguard Worker {
18*8975f5c5SAndroid Build Coastguard Worker 
19*8975f5c5SAndroid Build Coastguard Worker // Seed from clock
RNG()20*8975f5c5SAndroid Build Coastguard Worker RNG::RNG()
21*8975f5c5SAndroid Build Coastguard Worker {
22*8975f5c5SAndroid Build Coastguard Worker     long long timeSeed = std::chrono::system_clock::now().time_since_epoch().count();
23*8975f5c5SAndroid Build Coastguard Worker     mGenerator.seed(static_cast<unsigned int>(timeSeed));
24*8975f5c5SAndroid Build Coastguard Worker }
25*8975f5c5SAndroid Build Coastguard Worker 
26*8975f5c5SAndroid Build Coastguard Worker // Seed from fixed number.
RNG(unsigned int seed)27*8975f5c5SAndroid Build Coastguard Worker RNG::RNG(unsigned int seed) : mGenerator(seed) {}
28*8975f5c5SAndroid Build Coastguard Worker 
~RNG()29*8975f5c5SAndroid Build Coastguard Worker RNG::~RNG() {}
30*8975f5c5SAndroid Build Coastguard Worker 
reseed(unsigned int newSeed)31*8975f5c5SAndroid Build Coastguard Worker void RNG::reseed(unsigned int newSeed)
32*8975f5c5SAndroid Build Coastguard Worker {
33*8975f5c5SAndroid Build Coastguard Worker     mGenerator.seed(newSeed);
34*8975f5c5SAndroid Build Coastguard Worker }
35*8975f5c5SAndroid Build Coastguard Worker 
randomBool(float probTrue)36*8975f5c5SAndroid Build Coastguard Worker bool RNG::randomBool(float probTrue)
37*8975f5c5SAndroid Build Coastguard Worker {
38*8975f5c5SAndroid Build Coastguard Worker     std::bernoulli_distribution dist(probTrue);
39*8975f5c5SAndroid Build Coastguard Worker     return dist(mGenerator);
40*8975f5c5SAndroid Build Coastguard Worker }
41*8975f5c5SAndroid Build Coastguard Worker 
randomInt()42*8975f5c5SAndroid Build Coastguard Worker int RNG::randomInt()
43*8975f5c5SAndroid Build Coastguard Worker {
44*8975f5c5SAndroid Build Coastguard Worker     std::uniform_int_distribution<int> intDistribution;
45*8975f5c5SAndroid Build Coastguard Worker     return intDistribution(mGenerator);
46*8975f5c5SAndroid Build Coastguard Worker }
47*8975f5c5SAndroid Build Coastguard Worker 
randomIntBetween(int min,int max)48*8975f5c5SAndroid Build Coastguard Worker int RNG::randomIntBetween(int min, int max)
49*8975f5c5SAndroid Build Coastguard Worker {
50*8975f5c5SAndroid Build Coastguard Worker     std::uniform_int_distribution<int> intDistribution(min, max);
51*8975f5c5SAndroid Build Coastguard Worker     return intDistribution(mGenerator);
52*8975f5c5SAndroid Build Coastguard Worker }
53*8975f5c5SAndroid Build Coastguard Worker 
randomUInt()54*8975f5c5SAndroid Build Coastguard Worker unsigned int RNG::randomUInt()
55*8975f5c5SAndroid Build Coastguard Worker {
56*8975f5c5SAndroid Build Coastguard Worker     std::uniform_int_distribution<unsigned int> uintDistribution;
57*8975f5c5SAndroid Build Coastguard Worker     return uintDistribution(mGenerator);
58*8975f5c5SAndroid Build Coastguard Worker }
59*8975f5c5SAndroid Build Coastguard Worker 
randomFloat()60*8975f5c5SAndroid Build Coastguard Worker float RNG::randomFloat()
61*8975f5c5SAndroid Build Coastguard Worker {
62*8975f5c5SAndroid Build Coastguard Worker     std::uniform_real_distribution<float> floatDistribution;
63*8975f5c5SAndroid Build Coastguard Worker     return floatDistribution(mGenerator);
64*8975f5c5SAndroid Build Coastguard Worker }
65*8975f5c5SAndroid Build Coastguard Worker 
randomFloatBetween(float min,float max)66*8975f5c5SAndroid Build Coastguard Worker float RNG::randomFloatBetween(float min, float max)
67*8975f5c5SAndroid Build Coastguard Worker {
68*8975f5c5SAndroid Build Coastguard Worker     std::uniform_real_distribution<float> floatDistribution(min, max);
69*8975f5c5SAndroid Build Coastguard Worker     return floatDistribution(mGenerator);
70*8975f5c5SAndroid Build Coastguard Worker }
71*8975f5c5SAndroid Build Coastguard Worker 
randomFloatNonnegative()72*8975f5c5SAndroid Build Coastguard Worker float RNG::randomFloatNonnegative()
73*8975f5c5SAndroid Build Coastguard Worker {
74*8975f5c5SAndroid Build Coastguard Worker     std::uniform_real_distribution<float> floatDistribution(0.0f,
75*8975f5c5SAndroid Build Coastguard Worker                                                             std::numeric_limits<float>::max());
76*8975f5c5SAndroid Build Coastguard Worker     return floatDistribution(mGenerator);
77*8975f5c5SAndroid Build Coastguard Worker }
78*8975f5c5SAndroid Build Coastguard Worker 
randomNegativeOneToOne()79*8975f5c5SAndroid Build Coastguard Worker float RNG::randomNegativeOneToOne()
80*8975f5c5SAndroid Build Coastguard Worker {
81*8975f5c5SAndroid Build Coastguard Worker     return randomFloatBetween(-1.0f, 1.0f);
82*8975f5c5SAndroid Build Coastguard Worker }
83*8975f5c5SAndroid Build Coastguard Worker 
84*8975f5c5SAndroid Build Coastguard Worker }  // namespace angle
85