Skip to content

Commit 6f44bc9

Browse files
committed
Added new random methods.
1 parent 5c440e3 commit 6f44bc9

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

Runtime/RandomHelper.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public static int Seed
4242

4343
private static int _seed = 0;
4444

45+
private readonly static char[] _symbols = new char[]
46+
{
47+
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':',
48+
';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'
49+
};
50+
4551
private const float PI_2 = Mathf.PI * 2f;
4652

4753
#endregion
@@ -68,6 +74,40 @@ public static bool NextBool()
6874
return RNG.Next(1) == 0;
6975
}
7076

77+
/// <summary>
78+
/// Retrieve a random lowercase letter.
79+
/// </summary>
80+
/// <returns></returns>
81+
[Pure]
82+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
83+
public static char NextLetter()
84+
{
85+
return (char)Range(97, 123);
86+
}
87+
88+
/// <summary>
89+
/// Retrieve a random symbol.
90+
/// </summary>
91+
/// <returns></returns>
92+
[Pure]
93+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
94+
public static char NextSymbol()
95+
{
96+
return Choose(_symbols);
97+
}
98+
99+
/// <summary>
100+
/// Retrieve a random digit.
101+
/// </summary>
102+
/// <param name="max"></param>
103+
/// <returns></returns>
104+
[Pure]
105+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
106+
public static int NextDigit()
107+
{
108+
return RNG.Next(10);
109+
}
110+
71111
/// <summary>
72112
/// Retrieve a random float value between 0 [inclusive] and 1 [exclusive].
73113
/// </summary>
@@ -91,6 +131,29 @@ public static float NextFloat(float max)
91131
return NextFloat() * max;
92132
}
93133

134+
/// <summary>
135+
/// Retrieve a random double value between 0 [inclusive] and 1 [exclusive].
136+
/// </summary>
137+
/// <returns></returns>
138+
[Pure]
139+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
140+
public static double NextDouble()
141+
{
142+
return RNG.NextDouble();
143+
}
144+
145+
/// <summary>
146+
/// Retrieve a random double between 0 [inclusive] and a max value [exclusive].
147+
/// </summary>
148+
/// <param name="max"></param>
149+
/// <returns></returns>
150+
[Pure]
151+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
152+
public static double NextDouble(double max)
153+
{
154+
return NextDouble() * max;
155+
}
156+
94157
/// <summary>
95158
/// Retrieve a random int between 0 [inclusive] and a max value [exclusive].
96159
/// </summary>
@@ -162,6 +225,19 @@ public static float Range(float min, float max)
162225
return min + NextFloat(max - min);
163226
}
164227

228+
/// <summary>
229+
/// Retrieve a random double between a min value [inclusive] and a max value [exclusive].
230+
/// </summary>
231+
/// <param name="min"></param>
232+
/// <param name="max"></param>
233+
/// <returns></returns>
234+
[Pure]
235+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
236+
public static double Range(double min, double max)
237+
{
238+
return min + NextDouble(max - min);
239+
}
240+
165241
/// <summary>
166242
/// Retrieve a random vector with components between a min value [inclusive] and a max value [exclusive].
167243
/// </summary>
@@ -222,6 +298,18 @@ public static bool Chance(float percent)
222298
return NextFloat() < percent;
223299
}
224300

301+
/// <summary>
302+
/// Roll a random chance.
303+
/// </summary>
304+
/// <param name="percent">0.0 - 1.0.</param>
305+
/// <returns></returns>
306+
[Pure]
307+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
308+
public static bool Chance(double percent)
309+
{
310+
return NextDouble() < percent;
311+
}
312+
225313
/// <summary>
226314
/// Roll a random chance.
227315
/// </summary>

0 commit comments

Comments
 (0)