-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNG.cs
106 lines (93 loc) · 3.62 KB
/
RNG.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
namespace Calcium;
/// <summary>
/// Collection of various random number generation and random chance methods. Previously, this was a static class, but it has now been modified to be instanced with an optional seed.
/// The various `Roll` methods can be invoked with `Roll(n)` using either float, double, or int values, or specified (for readability) with `Rollf(n)`, `Rolld(n)`, and `Rolli(n)`
/// </summary>
public static class RNG {
private static Random Random = new();
public static void SetSeed(int seed) {
Random = new Random(seed);
}
/// <summary>
/// Alias for `Random.Next()` with an optional maximum value
/// </summary>
/// <param name="max">The maximum value of the integer returned</param>
/// <returns>Random int between 0 and <paramref name="max"/></returns>
public static int Next(int max=int.MaxValue) {
return Random.Next(max);
}
/// <summary>
/// Alias for `Random.NextDouble()`
/// </summary>
/// <returns>Random double</returns>
public static double NextDouble() {
return Random.NextDouble();
}
/// <summary>
/// Return the result of an <paramref name="n"/>-sided dice roll (Shorthand for `Range(1, n)`)
/// </summary>
/// <param name="n">Highest number on the die to roll</param>
/// <returns>Result of the roll</returns>
public static int Roll(int n) {
return Range(1, n);
}
/// <summary>
/// Return a random double between 0 and n (inclusive)
/// </summary>
/// <param name="n">Maximum return value</param>
/// <returns>Double between 0 and n</returns>
public static double Roll(double n) {
return Random.NextDouble() * n;
}
/// <summary>
/// Return a random float between 0 and n (inclusive)
/// </summary>
/// <param name="n">Maximum return value</param>
/// <returns>Float between 0 and n</returns>
public static float Roll(float n) {
return (float)Roll((double)n);
}
// Return a random int from an inclusive range
/// <summary>
/// Returns a random integer from an inclusive range
/// </summary>
/// <param name="min">Lowest possible value</param>
/// <param name="max">Highest possible value</param>
/// <returns>Random int from <paramref name="min"/> to <paramref name="max"/> (inclusive)</returns>
public static int Range(int min, int max) {
return Random.Next(min, max+1);
}
/// <summary>
/// Return true if the result of a <paramref name="n"/>-sided die equals 1
/// Shorthand for `Range(1, n) == 1` or `Roll(n) == 1`
/// </summary>
/// <param name="n">Number of "sides"</param>
/// <returns>Result of the roll</returns>
public static bool Odds(int n) {
return Range(1, n) == 1;
}
/// <summary>
/// Return true if the result of a 100-sided die is less than or equal equal to <paramref name="n"/>
/// </summary>
/// <param name="n">% chance of sucess</param>
/// <returns>Boolean result of the % chance</returns>
public static bool Chance(int n) {
return Range(1, 100) <= n;
}
/// <summary>
/// Returns the result of a coin flip
/// Shorthand for `Roll(50)`
/// </summary>
/// <returns></returns>
public static bool CoinFlip() {
return Roll(2) == 1;
}
/// <summary>
/// Returns <paramref name="e"/> with it's contents in random order
/// </summary>
/// <param name="e">IEnumerable<> to shuffle</param>
/// <returns><paramref name="e"/> shuffled</returns>
public static IEnumerable<object> Shuffle(IEnumerable<object> e) {
return e.OrderBy(a => Random.Next()).ToList();
}
}