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
random.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <random>
5
6namespace asw::random {
7namespace {
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
14int random(int max)
15{
16 return between(0, max);
17}
18
19int 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
26float random(float max)
27{
28 return between(0.0F, max);
29}
30
31float 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
38bool chance()
39{
40 std::uniform_int_distribution dist(0, 1);
41 return dist(rng) == 1;
42}
43
44bool chance(float chance)
45{
46 std::uniform_real_distribution dist(0.0F, 1.0F);
47 return dist(rng) < chance;
48}
49} // namespace asw::random
int between(int min, int max)
Generate a random integer between min and max.
Definition random.cpp:19
bool chance()
Generate a random boolean.
Definition random.cpp:38
int random(int max)
Generate a random integer between 0 and max.
Definition random.cpp:14
Random module for the ASW library.