RandomMath.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // RandomMath.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. #endregion
  13. namespace NetRumble
  14. {
  15. /// <summary>
  16. /// Static methods to assist with random-number generation.
  17. /// </summary>
  18. static public class RandomMath
  19. {
  20. #region Random Singleton
  21. /// <summary>
  22. /// The Random object used for all of the random calls.
  23. /// </summary>
  24. private static Random random = new Random();
  25. public static Random Random
  26. {
  27. get { return random; }
  28. }
  29. #endregion
  30. #region Single Variations
  31. /// <summary>
  32. /// Generate a random floating-point value between the minimum and
  33. /// maximum values provided.
  34. /// </summary>
  35. /// <remarks>This is similar to the Random.Next method, substituting singles
  36. /// for integers.</remarks>
  37. /// <param name="minimum">The minimum value.</param>
  38. /// <param name="maximum">The maximum value.</param>
  39. /// <returns>A random floating-point value between the minimum and maximum v
  40. /// alues provided.</returns>
  41. public static float RandomBetween(float minimum, float maximum)
  42. {
  43. return minimum + (float)random.NextDouble() * (maximum - minimum);
  44. }
  45. #endregion
  46. #region Direction Generation
  47. /// <summary>
  48. /// Generate a random direction vector.
  49. /// </summary>
  50. /// <returns>A random direction vector in 2D space.</returns>
  51. public static Vector2 RandomDirection()
  52. {
  53. float angle = RandomBetween(0, MathHelper.TwoPi);
  54. return new Vector2((float)Math.Cos(angle),
  55. (float)Math.Sin(angle));
  56. }
  57. /// <summary>
  58. /// Generate a random direction vector within constraints.
  59. /// </summary>
  60. /// <param name="minimumAngle">The minimum angle.</param>
  61. /// <param name="maximumAngle">The maximum angle.</param>
  62. /// <returns>
  63. /// A random direction vector in 2D space, within the constraints.
  64. /// </returns>
  65. public static Vector2 RandomDirection(float minimumAngle, float maximumAngle)
  66. {
  67. float angle = RandomBetween(MathHelper.ToRadians(minimumAngle),
  68. MathHelper.ToRadians(maximumAngle)) - MathHelper.PiOver2;
  69. return new Vector2((float)Math.Cos(angle),
  70. (float)Math.Sin(angle));
  71. }
  72. #endregion
  73. }
  74. }