RandomHelper.cs 752 B

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