[关闭]
@zhouweicsu 2017-05-29T22:09:04.000000Z 字数 2104 阅读 708

在此处输入标题

未分类


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace BoxmullerTest
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. try{
  12. FileStream fs = new FileStream("E://data.txt",FileMode.Create);
  13. //获得字节数组
  14. double E =20000 ;//正态分布期望
  15. double D =6000;//正态分布方差
  16. double[] ZTFBArr = new double[1000];
  17. int[] ZTFBArrint = new int[1000];
  18. int[] YSArr = new int[1000];
  19. Random u1 = new Random(); Random u2 = new Random();
  20. for (int i = 0; i < 1000; i++)
  21. {
  22. double? result = GetZTFB(u1.NextDouble(), u2.NextDouble(), E, D);
  23. if (result != null){
  24. ZTFBArr[i] = (double)result;
  25. ZTFBArrint[i] = (int)result;
  26. YSArr[i] = 98593110 % ZTFBArrint[i];
  27. byte [] data =new UTF8Encoding().GetBytes(String);
  28. //开始写入
  29. fs.Write(data,0,data.Length);
  30. }
  31. }
  32. //清空缓冲区、关闭流
  33. fs.Flush();
  34. fs.Close();
  35. }
  36. catch (IOException e);
  37. {
  38. Console.WriteLine("An IO exception has been thrown!");
  39. Console.WriteLine(e.ToString());
  40. Console.ReadLine();
  41. return;
  42. }
  43. Console.WriteLine(ZTFBArr.Length);
  44. Console.ReadKey();
  45. for (int i = 0; i < ZTFBArr.Length; i++)
  46. {
  47. Console.Write(string.Format("{0} ",YSArr[i]));
  48. }
  49. Console.ReadKey();
  50. double teste = GetE(ZTFBArr);
  51. double testd = GetD(ZTFBArr, teste);
  52. Console.WriteLine(string.Format("计算期望:{0} 生成期望:{1}", E, teste));
  53. Console.WriteLine(string.Format("计算方差:{0} 生成方差:{1}", D, testd));
  54. Console.ReadKey();
  55. }
  56. /// <summary>
  57. /// 计算数组期望值
  58. /// </summary>
  59. /// <param name="arr"></param>
  60. /// <returns></returns>
  61. private static double GetE(double[] arr)
  62. {
  63. double teste;//测试方差
  64. double sumresult = 0;
  65. for (int i = 0; i < arr.Length; i++)
  66. {
  67. sumresult += arr[i];
  68. }
  69. teste = sumresult / arr.Length;
  70. return teste;
  71. }
  72. /// <summary>
  73. /// 计算数组的方差
  74. /// </summary>
  75. /// <param name="arr"></param>
  76. /// <param name="teste">期望值</param>
  77. /// <returns></returns>
  78. private static double GetD(double[] arr, double teste)
  79. {
  80. double sumd = 0;
  81. for (int i = 0; i < arr.Length; i++)
  82. {
  83. sumd += Math.Pow(arr[i] - teste, 2);
  84. }
  85. return sumd / arr.Length;
  86. }
  87. /// <summary>
  88. /// 返回正态分布的值
  89. /// </summary>
  90. /// <param name="u1">第一个均匀分布值</param>
  91. /// <param name="u2">第二个均匀分布值</param>
  92. /// <param name="e">正态期望</param>
  93. /// <param name="d">正态方差</param>
  94. /// <returns>分布值或者null</returns>
  95. private static double? GetZTFB(double u1, double u2, double e, double d)
  96. {
  97. double? result = null;
  98. try
  99. {
  100. result = e + Math.Sqrt(d) * Math.Sqrt((-2) * (Math.Log(u1) / Math.Log(Math.E))) * Math.Sin(2 * Math.PI * u2);
  101. }
  102. catch (Exception ex)
  103. {
  104. result = null;
  105. }
  106. return result;
  107. }
  108. }
  109. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注