RandomExtensions.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. namespace MonoGame.Extended
  4. {
  5. public static class RandomExtensions
  6. {
  7. public static int Next(this Random random, Range<int> range)
  8. {
  9. return random.Next(range.Min, range.Max);
  10. }
  11. public static float NextSingle(this Random random, float min, float max)
  12. {
  13. return (max - min)*NextSingle(random) + min;
  14. }
  15. public static float NextSingle(this Random random, float max)
  16. {
  17. return max*NextSingle(random);
  18. }
  19. public static float NextSingle(this Random random)
  20. {
  21. return (float) random.NextDouble();
  22. }
  23. public static float NextSingle(this Random random, Range<float> range)
  24. {
  25. return NextSingle(random, range.Min, range.Max);
  26. }
  27. public static float NextAngle(this Random random)
  28. {
  29. return NextSingle(random, -MathHelper.Pi, MathHelper.Pi);
  30. }
  31. public static void NextUnitVector(this Random random, out Vector2 vector)
  32. {
  33. var angle = NextAngle(random);
  34. vector = new Vector2((float) Math.Cos(angle), (float) Math.Sin(angle));
  35. }
  36. }
  37. }