UnityEssentials
Small but useful tools and features for Unity
RandomEssentials.cs
Go to the documentation of this file.
1 using System;
2 
3 namespace UnityEngine
4 {
8  public class RandomEssentials : System.Random
9  {
10 
11  #region Constructors
12 
18  {
19  }
20 
27  public RandomEssentials(int seed) : base(seed)
28  {
29  }
30 
36  public static RandomEssentials GetNew()
37  {
38  return new RandomEssentials();
39  }
40 
46  public static RandomEssentials GetNew(int seed)
47  {
48  return new RandomEssentials(seed);
49  }
50 
51  #endregion
52 
53  #region Int
54 
60  public int GetRandomInt()
61  {
62  return Next();
63  }
64 
70  public int GetRandomInt(int exclusiveMaximum)
71  {
72  return Next(exclusiveMaximum);
73  }
74 
81  public int GetRandomInt(int inclusiveMinimum, int exclusiveMaximum)
82  {
83  return Next(inclusiveMinimum, exclusiveMaximum);
84  }
85 
86  #endregion
87 
88  #region Sign
89 
95  public int GetRandomSign(float negativeProbability = 0.5f)
96  {
97  return GetRandomBool(negativeProbability) ? -1 : 1;
98  }
99 
100  #endregion
101 
102  #region Bool
103 
109  public bool GetRandomBool(float probability = 0.5f)
110  {
111  return NextDouble() >= 1 - probability;
112  }
113 
120  public bool GetRandomBoolTrueEnsured(int tryNumberSinceLastPositive, int maxNumberOfTries)
121  {
122  return GetRandomBool((float) tryNumberSinceLastPositive / (float) maxNumberOfTries);
123  }
124 
133  public bool GetPseudoRandomDistributedBool(int tryNumberSinceLastPositive, float probability)
134  {
135  return GetRandomBool(Mathf.Pow(probability, 1.72689f) * tryNumberSinceLastPositive);
136 
137  // An image of how the results should distribute (in blue): https://gamepedia.cursecdn.com/dota2_gamepedia/8/8b/AttacksUntilNextProc25.jpg?version=459537150af02c929fd939495fa78033
138 
139  /* The true formula to calculate each try's probability is: "C * tryNumber"
140  // In this method's implementation, "C" is calculated using the formula: "probability^1.72689"
141  // However, the accuracy of the results could be improved by using this table instead of the previous calculus:
142  Probability Associated "C"
143  5% 0.003801658303553139101756466
144  10% 0.014745844781072675877050816
145  15% 0.032220914373087674975117359
146  20% 0.055704042949781851858398652
147  25% 0.084744091852316990275274806
148  30% 0.118949192725403987583755553
149  35% 0.157983098125747077557540462
150  40% 0.201547413607754017070679639
151  45% 0.249306998440163189714677100
152  50% 0.302103025348741965169160432
153  55% 0.360397850933168697104686803
154  60% 0.422649730810374235490851220
155  65% 0.481125478337229174401911323
156  70% 0.571428571428571428571428572
157  75% 0.666666666666666666666666667
158  80% 0.750000000000000000000000000
159  85% 0.823529411764705882352941177
160  90% 0.888888888888888888888888889
161  95% 0.947368421052631578947368421
162  */
163  }
164 
165  #endregion
166 
167  #region Float
168 
173  public float GetRandomFloat()
174  {
175  return Convert.ToSingle(NextDouble());
176  }
177 
183  public float GetRandomFloat(float exclusiveMaximum)
184  {
185  return Convert.ToSingle(NextDouble() * exclusiveMaximum);
186  }
187 
194  public float GetRandomFloat(float inclusiveMinimum, float exclusiveMaximum)
195  {
196  return Convert.ToSingle(NextDouble() * (exclusiveMaximum - inclusiveMinimum)) + inclusiveMinimum;
197  }
198 
199  #endregion
200 
201  #region Vectors
202 
209  public Vector3 GetRandomVector3(float inclusiveMinimum, float exclusiveMaximum)
210  {
211  return new Vector3(GetRandomFloat(inclusiveMinimum, exclusiveMaximum), GetRandomFloat(inclusiveMinimum, exclusiveMaximum), GetRandomFloat(inclusiveMinimum, exclusiveMaximum));
212  }
213 
220  public Vector2 GetRandomVector2(float inclusiveMinimum, float exclusiveMaximum)
221  {
222  return new Vector2(GetRandomFloat(inclusiveMinimum, exclusiveMaximum), GetRandomFloat(inclusiveMinimum, exclusiveMaximum));
223  }
224 
225  #endregion
226 
227  #region Bytes
228 
233  public void SetRandomBytes(Byte[] bytesArray)
234  {
235  NextBytes(bytesArray);
236  }
237 
238  #endregion
239 
240  #region Geometry
241 
248  public Vector2 GetPointInsideCircle(Vector2 center, float radius = 1f)
249  {
250  double angle = this.NextDouble() * 2 * Math.PI;
251  double r = radius * Math.Sqrt(NextDouble());
252  double x = center.x + r * Math.Cos(angle);
253  double y = center.y + r * Math.Sin(angle);
254  return new Vector2( Convert.ToSingle(x), Convert.ToSingle(y) );
255  }
256 
262  public Vector2 GetPointInsideCircle( float radius = 1f)
263  {
264  return GetPointInsideCircle(Vector2.zero, radius);
265  }
266 
267 
274  public Vector2 GetPointOnCircle(Vector2 center, float radius = 1f)
275  {
276  double angle = 2.0 * Math.PI * this.NextDouble();
277  double x = center.x + radius * Math.Cos(angle);
278  double y = center.y + radius * Math.Sin(angle);
279  return new Vector2( Convert.ToSingle(x), Convert.ToSingle(y) );
280  }
281 
287  public Vector2 GetPointOnCircle( float radius = 1f)
288  {
289  return GetPointInsideCircle(Vector2.zero, radius);
290  }
291 
292  // Similar methods to the ones Unity's Random class has
293  //To-do: public Vector3 GetPointInsideSphere(float radius = 1){} //Returns a random point inside a sphere
294  //To-do: public Vector3 GetPointOnSphere(float radius = 1){} //Returns a random point on the surface of a sphere
295 
296  // Similar methods to the ones Unity's Random class has
297  //To-do: public Quaternion GetRotation(){} //Returns a random rotation
298  //To-do: public Quaternion GetRotationUniform(){} //Returns a random rotation with uniform distribution
299 
300  #endregion
301 
302  #region Color
303 
304  public Color GetColorHSV(float hueMin = 0.0f, float hueMax = 1f, float saturationMin = 0.0f, float saturationMax = 1f, float valueMin = 0.0f, float valueMax = 1f, float alphaMin = 1f, float alphaMax = 1f)
305  {
306  Color rgb = Color.HSVToRGB(Mathf.Lerp(hueMin, hueMax, GetRandomFloat()), Mathf.Lerp(saturationMin, saturationMax, GetRandomFloat()), Mathf.Lerp(valueMin, valueMax, GetRandomFloat()), true);
307  rgb.a = Mathf.Lerp(alphaMin, alphaMax, GetRandomFloat());
308  return rgb;
309  }
310 
311  #endregion
312 
313 
314  }
315 }
An easier to use and a feature-rich class to generate pseudo-random results.
Vector2 GetPointInsideCircle(float radius=1f)
Returns a random point inside a circle with center (0, 0).
RandomEssentials(int seed)
Creates a pseudo-random number generator, which is an algorithm that produces a sequence of numbers t...
float GetRandomFloat()
Returns a random float between 0 (included) and 1 (excluded)
int GetRandomInt(int inclusiveMinimum, int exclusiveMaximum)
Returns a random integer that is within a specified range.
static RandomEssentials GetNew()
Creates a pseudo-random number generator, which is an algorithm that produces a sequence of numbers t...
Vector2 GetRandomVector2(float inclusiveMinimum, float exclusiveMaximum)
Returns a random Vector2 with each parameter within a specified range.
bool GetPseudoRandomDistributedBool(int tryNumberSinceLastPositive, float probability)
Returns a random bool using pseudo-random distribution.
Vector2 GetPointOnCircle(float radius=1f)
Returns a random point inside a circle with center (0, 0).
void SetRandomBytes(Byte[] bytesArray)
Fills the elements of a specified array of bytes with random numbers.
float GetRandomFloat(float inclusiveMinimum, float exclusiveMaximum)
Returns a random float that is within a specified range.
Color GetColorHSV(float hueMin=0.0f, float hueMax=1f, float saturationMin=0.0f, float saturationMax=1f, float valueMin=0.0f, float valueMax=1f, float alphaMin=1f, float alphaMax=1f)
Vector2 GetPointInsideCircle(Vector2 center, float radius=1f)
Returns a random point inside a circle.
static RandomEssentials GetNew(int seed)
Creates a pseudo-random number generator, which is an algorithm that produces a sequence of numbers t...
int GetRandomInt(int exclusiveMaximum)
Returns a non-negative random integer that is less than the specified maximum.
RandomEssentials()
Creates a pseudo-random number generator, which is an algorithm that produces a sequence of numbers t...
float GetRandomFloat(float exclusiveMaximum)
Returns a random float that is less than the specified maximum.
Vector3 GetRandomVector3(float inclusiveMinimum, float exclusiveMaximum)
Returns a random Vector3 with each parameter within a specified range.
bool GetRandomBoolTrueEnsured(int tryNumberSinceLastPositive, int maxNumberOfTries)
Returns a random bool that will be true before the maximum number of tries is exceeded.
int GetRandomSign(float negativeProbability=0.5f)
Returns a random sign as -1 or +1.
bool GetRandomBool(float probability=0.5f)
Returns a random bool.
Vector2 GetPointOnCircle(Vector2 center, float radius=1f)
Returns a random point on a circle.