StaticMath.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Island.StandardLib.Math
  6. {
  7. public static class StaticMath
  8. {
  9. static Random random = new Random();
  10. public static int RandI(int min, int max) => random.Next(min, max);
  11. public static float RandF(float min, float max) => (min + max) * 0.5f + (float)(random.NextDouble() * 2d - 1d) * (max - min) / 2;
  12. public static long RandL(long min, long max)
  13. {
  14. long Max = max, Min = min;
  15. if (min > max)
  16. {
  17. Max = min;
  18. Min = max;
  19. }
  20. double Key = random.NextDouble();
  21. long myResult = Min + (long)((Max - Min) * Key);
  22. return myResult;
  23. }
  24. public static float DisturbContinuous(this float input, float factor01, float range_percentage = 0.01f) => input + input * (float)(factor01 * 2d - 1d) * range_percentage;
  25. public static float Disturb(this float input, float range_percentage = 0.01f) => input + input * (float)(random.NextDouble() * 2d - 1d) * range_percentage;
  26. public static int Disturb(this int input, float range_percentage = 0.01f) => random.Next((int)(input * (1f - range_percentage)), (int)(input * (1f + range_percentage)));
  27. public static long Disturb(this long input, float range_percentage = 0.01f) => RandL((long)(input * (1f - range_percentage)), (long)(input * (1f + range_percentage)));
  28. }
  29. }