-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom.cpp
51 lines (42 loc) · 1.13 KB
/
Random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "Random.h"
void Random::Init()
{
std::random_device rd;
Random::Seed(rd());
}
void Random::Seed(unsigned int seed)
{
sGenerator.seed(seed);
}
float Random::GetFloat()
{
return GetFloatRange(0.0f, 1.0f);
}
float Random::GetFloatRange(float min, float max)
{
std::uniform_real_distribution<float> dist(min, max);
return dist(sGenerator);
}
int Random::GetIntRange(int min, int max)
{
std::uniform_int_distribution<int> dist(min, max);
return dist(sGenerator);
}
Vector2 Random::GetVector(const Vector2& min, const Vector2& max)
{
Vector2 r = Vector2(GetFloat(), GetFloat());
return min + (max - min) * r;
}
Vector3 Random::GetVector(const Vector3& min, const Vector3& max)
{
Vector3 r = Vector3(GetFloat(), GetFloat(), GetFloat());
return min + (max - min) * r;
}
std::mt19937 Random::sGenerator;