RandomHelper.cs 877 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace XdCxRhDW.Framework
  7. {
  8. public static class RandomHelper
  9. {
  10. //标准正态分布
  11. public static double Normal()
  12. {
  13. Random aa = new Random(Guid.NewGuid().ToString().GetHashCode());
  14. double s = 0, u = 0, v = 0;
  15. while (s > 1 || s == 0)
  16. {
  17. u = aa.NextDouble() * 2 - 1;
  18. v = aa.NextDouble() * 2 - 1;
  19. s = u * u + v * v;
  20. }
  21. var z = Math.Sqrt(-2 * Math.Log(s) / s) * u;
  22. return (z);
  23. }
  24. //符合要求的正态分布随机数
  25. public static double Normal(double miu, double sigma)
  26. {
  27. var z = Normal() * sigma + miu;
  28. return (z);
  29. }
  30. }
  31. }