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.
7 // Helper functions for random number generation.
10 #include "random_utils.h"
22 long long timeSeed = std::chrono::system_clock::now().time_since_epoch().count();
23 mGenerator.seed(static_cast<unsigned int>(timeSeed));
26 // Seed from fixed number.
27 RNG::RNG(unsigned int seed) : mGenerator(seed)
35 void RNG::reseed(unsigned int newSeed)
37 mGenerator.seed(newSeed);
42 std::uniform_int_distribution<int> intDistribution;
43 return intDistribution(mGenerator);
46 int RNG::randomIntBetween(int min, int max)
48 std::uniform_int_distribution<int> intDistribution(min, max);
49 return intDistribution(mGenerator);
52 unsigned int RNG::randomUInt()
54 std::uniform_int_distribution<unsigned int> uintDistribution;
55 return uintDistribution(mGenerator);
58 float RNG::randomFloat()
60 std::uniform_real_distribution<float> floatDistribution;
61 return floatDistribution(mGenerator);
64 float RNG::randomFloatBetween(float min, float max)
66 std::uniform_real_distribution<float> floatDistribution(min, max);
67 return floatDistribution(mGenerator);
70 float RNG::randomNegativeOneToOne()
72 return randomFloatBetween(-1.0f, 1.0f);