ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
src
modules
random.cpp
Go to the documentation of this file.
1
#include "
./asw/modules/random.h
"
2
3
#include <algorithm>
4
#include <random>
5
6
namespace
asw::random
{
7
namespace
{
8
// Random number generator
9
std::random_device
rd
;
10
std::mt19937
rng
(
rd
());
11
}
// namespace
12
13
// The std distributions are undefined when min > max, so order the range
14
int
random
(
int
max)
15
{
16
return
between
(0, max);
17
}
18
19
int
between
(
int
min,
int
max)
20
{
21
const
auto
[lo, hi] = std::minmax(min, max);
22
std::uniform_int_distribution dist(lo, hi);
23
return
dist(rng);
24
}
25
26
float
random
(
float
max)
27
{
28
return
between
(0.0F, max);
29
}
30
31
float
between
(
float
min,
float
max)
32
{
33
const
auto
[lo, hi] = std::minmax(min, max);
34
std::uniform_real_distribution dist(lo, hi);
35
return
dist(rng);
36
}
37
38
bool
chance
()
39
{
40
std::uniform_int_distribution dist(0, 1);
41
return
dist(rng) == 1;
42
}
43
44
bool
chance
(
float
chance
)
45
{
46
std::uniform_real_distribution dist(0.0F, 1.0F);
47
return
dist(rng) <
chance
;
48
}
49
}
// namespace asw::random
asw::random::anonymous_namespace{random.cpp}::rng
std::mt19937 rng(rd())
asw::random::anonymous_namespace{random.cpp}::rd
std::random_device rd
Definition
random.cpp:9
asw::random
Definition
random.cpp:6
asw::random::between
int between(int min, int max)
Generate a random integer between min and max.
Definition
random.cpp:19
asw::random::chance
bool chance()
Generate a random boolean.
Definition
random.cpp:38
asw::random::random
int random(int max)
Generate a random integer between 0 and max.
Definition
random.cpp:14
random.h
Random module for the ASW library.
Generated by
1.12.0