RandomHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //-----------------------------------------------------------------------------
  2. // RandomHelper.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. namespace RacingGame.Helpers
  13. {
  14. /// <summary>
  15. /// Random helper
  16. /// </summary>
  17. public static class RandomHelper
  18. {
  19. /// <summary>
  20. /// Global random generator
  21. /// </summary>
  22. public static Random globalRandomGenerator =
  23. GenerateNewRandomGenerator();
  24. /// <summary>
  25. /// Generate a new random generator with help of
  26. /// WindowsHelper.GetPerformanceCounter.
  27. /// Also used for all GetRandom methods here.
  28. /// </summary>
  29. /// <returns>Random</returns>
  30. public static Random GenerateNewRandomGenerator()
  31. {
  32. globalRandomGenerator =
  33. new Random((int)DateTime.Now.Ticks);
  34. //needs Interop: (int)WindowsHelper.GetPerformanceCounter());
  35. return globalRandomGenerator;
  36. }
  37. /// <summary>
  38. /// Get random int
  39. /// </summary>
  40. /// <param name="max">Maximum</param>
  41. /// <returns>Int</returns>
  42. public static int GetRandomInt(int max)
  43. {
  44. return globalRandomGenerator.Next(max);
  45. }
  46. /// <summary>
  47. /// Get random float between min and max
  48. /// </summary>
  49. /// <param name="min">Min</param>
  50. /// <param name="max">Max</param>
  51. /// <returns>Float</returns>
  52. public static float GetRandomFloat(float min, float max)
  53. {
  54. return (float)globalRandomGenerator.NextDouble() * (max - min) + min;
  55. }
  56. /// <summary>
  57. /// Get random byte between min and max
  58. /// </summary>
  59. /// <param name="min">Min</param>
  60. /// <param name="max">Max</param>
  61. /// <returns>Byte</returns>
  62. public static byte GetRandomByte(byte min, byte max)
  63. {
  64. return (byte)(globalRandomGenerator.Next(min, max));
  65. }
  66. /// <summary>
  67. /// Get random Vector2
  68. /// </summary>
  69. /// <param name="min">Minimum for each component</param>
  70. /// <param name="max">Maximum for each component</param>
  71. /// <returns>Vector2</returns>
  72. public static Vector2 GetRandomVector2(float min, float max)
  73. {
  74. return new Vector2(
  75. GetRandomFloat(min, max),
  76. GetRandomFloat(min, max));
  77. }
  78. /// <summary>
  79. /// Get random Vector3
  80. /// </summary>
  81. /// <param name="min">Minimum for each component</param>
  82. /// <param name="max">Maximum for each component</param>
  83. /// <returns>Vector3</returns>
  84. public static Vector3 GetRandomVector3(float min, float max)
  85. {
  86. return new Vector3(
  87. GetRandomFloat(min, max),
  88. GetRandomFloat(min, max),
  89. GetRandomFloat(min, max));
  90. }
  91. /// <summary>
  92. /// Get random color
  93. /// </summary>
  94. /// <returns>Color</returns>
  95. public static Color RandomColor
  96. {
  97. get
  98. {
  99. return new Color(new Vector3(
  100. GetRandomFloat(0.25f, 1.0f),
  101. GetRandomFloat(0.25f, 1.0f),
  102. GetRandomFloat(0.25f, 1.0f)));
  103. }
  104. }
  105. /// <summary>
  106. /// Get random normal Vector3
  107. /// </summary>
  108. /// <returns>Vector3</returns>
  109. public static Vector3 RandomNormalVector3
  110. {
  111. get
  112. {
  113. Vector3 randomNormalVector = new Vector3(
  114. GetRandomFloat(-1.0f, 1.0f),
  115. GetRandomFloat(-1.0f, 1.0f),
  116. GetRandomFloat(-1.0f, 1.0f));
  117. randomNormalVector.Normalize();
  118. return randomNormalVector;
  119. }
  120. }
  121. }
  122. }