-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathRandomMath.cs
94 lines (74 loc) · 2.83 KB
/
RandomMath.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
#region File Description
//-----------------------------------------------------------------------------
// RandomMath.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework;
#endregion
namespace NetRumble
{
/// <summary>
/// Static methods to assist with random-number generation.
/// </summary>
static public class RandomMath
{
#region Random Singleton
/// <summary>
/// The Random object used for all of the random calls.
/// </summary>
private static Random random = new Random();
public static Random Random
{
get { return random; }
}
#endregion
#region Single Variations
/// <summary>
/// Generate a random floating-point value between the minimum and
/// maximum values provided.
/// </summary>
/// <remarks>This is similar to the Random.Next method, substituting singles
/// for integers.</remarks>
/// <param name="minimum">The minimum value.</param>
/// <param name="maximum">The maximum value.</param>
/// <returns>A random floating-point value between the minimum and maximum v
/// alues provided.</returns>
public static float RandomBetween(float minimum, float maximum)
{
return minimum + (float)random.NextDouble() * (maximum - minimum);
}
#endregion
#region Direction Generation
/// <summary>
/// Generate a random direction vector.
/// </summary>
/// <returns>A random direction vector in 2D space.</returns>
public static Vector2 RandomDirection()
{
float angle = RandomBetween(0, MathHelper.TwoPi);
return new Vector2((float)Math.Cos(angle),
(float)Math.Sin(angle));
}
/// <summary>
/// Generate a random direction vector within constraints.
/// </summary>
/// <param name="minimumAngle">The minimum angle.</param>
/// <param name="maximumAngle">The maximum angle.</param>
/// <returns>
/// A random direction vector in 2D space, within the constraints.
/// </returns>
public static Vector2 RandomDirection(float minimumAngle, float maximumAngle)
{
float angle = RandomBetween(MathHelper.ToRadians(minimumAngle),
MathHelper.ToRadians(maximumAngle)) - MathHelper.PiOver2;
return new Vector2((float)Math.Cos(angle),
(float)Math.Sin(angle));
}
#endregion
}
}