styyx-util 1.0
Utility Header for SKSE Plugin development
Loading...
Searching...
No Matches
st-random.h
Go to the documentation of this file.
1#pragma once
2
3namespace StyyxUtil
4{
5
7{
12 static int GetRandomInt(const int a_min, const int a_max)
13 {
14 std::uniform_int_distribution<int> distrib(a_min, a_max);
15 return distrib(GetRNG());
16 }
17
22 static float GetRandomFloat(const float a_min, const float a_max)
23 {
24 std::uniform_real_distribution<float> distrib(a_min, a_max);
25 return distrib(GetRNG());
26 }
27
33 static float GetRandomRoundedFloat(const float a_min, const float a_max, const int decimals = 2)
34 {
35 const auto value = GetRandomFloat(a_min, a_max);
36 const double factor = std::pow(10.0, static_cast<double>(decimals));
37 return static_cast<float>(std::round(value * factor) / factor);
38 }
39
44 static double GetRandomDouble(const double a_min, const double a_max)
45 {
46 std::uniform_real_distribution<double> distrib(a_min, a_max);
47 return distrib(GetRNG());
48 }
49
52 static bool GetRandomBool()
53 {
54 std::bernoulli_distribution distrib(0.5);
55 return distrib(GetRNG());
56 }
57
62 static bool GetWeightedRandomBool(const float p = 0.5)
63 {
64 std::bernoulli_distribution distrib(p);
65 return distrib(GetRNG());
66 }
67
72 static bool IsPercentageChanceFloat(const float a_chancePercent)
73 {
74 const auto chance = std::clamp(a_chancePercent, 0.0f, 100.0f);
75
76 if (chance <= 0.0f)
77 return false;
78 if (chance >= 100.0f)
79 return true;
80 return GetRandomFloat(0.0, 100.0) <= a_chancePercent;
81 }
82
83 private:
84 static std::mt19937& GetRNG()
85 {
86 static std::mt19937 gen(std::random_device{}());
87 return gen;
88 }
89};
90} // namespace StyyxUtil
Definition st-actor.h:7
Definition st-random.h:7
static int GetRandomInt(const int a_min, const int a_max)
Get random integer between 2 values.
Definition st-random.h:12
static std::mt19937 & GetRNG()
Definition st-random.h:84
static double GetRandomDouble(const double a_min, const double a_max)
Get random double between 2 values.
Definition st-random.h:44
static bool GetWeightedRandomBool(const float p=0.5)
Get weighted random bool.
Definition st-random.h:62
static bool IsPercentageChanceFloat(const float a_chancePercent)
Rolls a random chance check against a given percentage.
Definition st-random.h:72
static float GetRandomRoundedFloat(const float a_min, const float a_max, const int decimals=2)
Get random float rounded to a set amount of decimals.
Definition st-random.h:33
static float GetRandomFloat(const float a_min, const float a_max)
Get random float between 2 values.
Definition st-random.h:22
static bool GetRandomBool()
Get random bool.
Definition st-random.h:52