Skip to content

Commit e9b4860

Browse files
committed
Added methods to do with random.
1 parent 5a5b0b9 commit e9b4860

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

Runtime/RandomHelper.cs

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,234 @@
22
// Purpose: Various static helper methods to do with randomness
33
// Created by: DavidFDev
44

5+
using System;
6+
using System.Collections.Generic;
7+
using System.Diagnostics.Contracts;
8+
using System.Linq;
9+
using UnityEngine;
10+
511
namespace DavidFDev.Maths
612
{
13+
/// <summary>
14+
/// Collection of static helper methods to do with randomness.
15+
/// </summary>
716
public static class RandomHelper
817
{
18+
#region Static fields and constants
19+
20+
/// <summary>
21+
/// Random number generator used by the helper methods.
22+
/// </summary>
23+
public static System.Random RNG { get; private set; } = null;
24+
25+
/// <summary>
26+
/// Seed used by the random number generator instance.
27+
/// Setting a new seed will initialise a new random number generator.
28+
/// </summary>
29+
public static int Seed
30+
{
31+
get
32+
{
33+
return _seed;
34+
}
35+
set
36+
{
37+
_seed = value;
38+
RNG = new System.Random(_seed);
39+
}
40+
}
41+
42+
private static int _seed = 0;
43+
44+
private const float PI_2 = Mathf.PI * 2f;
45+
46+
#endregion
47+
48+
#region Static constructor
49+
50+
static RandomHelper()
51+
{
52+
Seed = Environment.TickCount;
53+
}
54+
55+
#endregion
56+
57+
#region Static methods
58+
59+
/// <summary>
60+
/// Retrieve a random bool value.
61+
/// </summary>
62+
/// <returns></returns>
63+
[Pure]
64+
public static bool NextBool()
65+
{
66+
return RNG.Next(1) == 0;
67+
}
68+
69+
/// <summary>
70+
/// Retrieve a random float value between 0 [inclusive] and 1 [exclusive].
71+
/// </summary>
72+
/// <returns></returns>
73+
[Pure]
74+
public static float NextFloat()
75+
{
76+
return (float)RNG.NextDouble();
77+
}
78+
79+
/// <summary>
80+
/// Retrieve a random float between 0 [inclusive] and a max value [exclusive].
81+
/// </summary>
82+
/// <param name="max"></param>
83+
/// <returns></returns>
84+
[Pure]
85+
public static float NextFloat(float max)
86+
{
87+
return NextFloat() * max;
88+
}
89+
90+
/// <summary>
91+
/// Retrieve a random int between 0 [inclusive] and a max value [exclusive].
92+
/// </summary>
93+
/// <param name="max"></param>
94+
/// <returns></returns>
95+
[Pure]
96+
public static int NextInt(int max)
97+
{
98+
return RNG.Next(max);
99+
}
100+
101+
/// <summary>
102+
/// Retrieve a random angle between 0 and 2 PI (radians).
103+
/// </summary>
104+
/// <returns></returns>
105+
[Pure]
106+
public static float NextAngle()
107+
{
108+
return NextFloat(PI_2);
109+
}
110+
111+
/// <summary>
112+
/// Retrieve a colour with random component values (rgb).
113+
/// </summary>
114+
/// <returns></returns>
115+
[Pure]
116+
public static Color NextColour()
117+
{
118+
return new Color(NextFloat(), NextFloat(), NextFloat());
119+
}
120+
121+
/// <summary>
122+
/// Retrieve a random unit vector.
123+
/// </summary>
124+
/// <param name="magnitude"></param>
125+
/// <returns></returns>
126+
[Pure]
127+
public static Vector2 NextVector2(float magnitude = 1f)
128+
{
129+
return MathsHelper.GetDirection(NextAngle() * Mathf.Rad2Deg) * magnitude;
130+
}
131+
132+
/// <summary>
133+
/// Retrive a random unit vector.
134+
/// </summary>
135+
/// <param name="magnitude"></param>
136+
/// <returns></returns>
137+
[Pure]
138+
public static Vector3 NextVector3(float magnitude = 1f)
139+
{
140+
return MathsHelper.GetDirection(NextAngle() * Mathf.Rad2Deg) * magnitude;
141+
}
142+
143+
/// <summary>
144+
/// Retrieve a random float between a min value [inclusive] and a max value [exclusive].
145+
/// </summary>
146+
/// <param name="min"></param>
147+
/// <param name="max"></param>
148+
/// <returns></returns>
149+
[Pure]
150+
public static float Range(float min, float max)
151+
{
152+
return min + NextFloat(max - min);
153+
}
154+
155+
/// <summary>
156+
/// Retrieve a random vector with components between a min value [inclusive] and a max value [exclusive].
157+
/// </summary>
158+
/// <param name="min"></param>
159+
/// <param name="max"></param>
160+
/// <returns></returns>
161+
[Pure]
162+
public static Vector2 Range(Vector2 min, Vector2 max)
163+
{
164+
return min + new Vector2(NextFloat(max.x - min.x), NextFloat(max.y - min.y));
165+
}
166+
167+
/// <summary>
168+
/// Retrieve a random vector with components between a min value [inclusive] and a max value [exclusive].
169+
/// </summary>
170+
/// <param name="min"></param>
171+
/// <param name="max"></param>
172+
/// <returns></returns>
173+
[Pure]
174+
public static Vector3 Range(Vector3 min, Vector3 max)
175+
{
176+
return min + new Vector3(NextFloat(max.x - min.x), NextFloat(max.y - min.y), NextFloat(max.z - min.z));
177+
}
178+
179+
/// <summary>
180+
/// Retrieve a random float betwen -1 and 1.
181+
/// </summary>
182+
/// <returns></returns>
183+
[Pure]
184+
public static float MinusOneToOne()
185+
{
186+
return NextFloat(2f) - 1f;
187+
}
188+
189+
/// <summary>
190+
/// Retrieve a random integer, either -1 or 1.
191+
/// </summary>
192+
/// <returns></returns>
193+
[Pure]
194+
public static int MinusOneOrOne()
195+
{
196+
return NextBool() ? -1 : 1;
197+
}
198+
199+
/// <summary>
200+
/// Roll a random chance.
201+
/// </summary>
202+
/// <param name="percent">0.0 - 1.0.</param>
203+
/// <returns></returns>
204+
[Pure]
205+
public static bool Chance(float percent)
206+
{
207+
return NextFloat() < percent;
208+
}
209+
210+
/// <summary>
211+
/// Roll a random chance.
212+
/// </summary>
213+
/// <param name="percent">0 - 100.</param>
214+
/// <returns></returns>
215+
[Pure]
216+
public static bool Chance(int percent)
217+
{
218+
return NextInt(100) < percent;
219+
}
220+
221+
/// <summary>
222+
/// Retrieve a random element from a collection.
223+
/// </summary>
224+
/// <typeparam name="T"></typeparam>
225+
/// <param name="collection"></param>
226+
/// <returns></returns>
227+
[Pure]
228+
public static T Choose<T>(this IReadOnlyCollection<T> collection)
229+
{
230+
return collection.ElementAt(NextInt(collection.Count));
231+
}
232+
233+
#endregion
9234
}
10235
}

0 commit comments

Comments
 (0)