正态分布

作者:追风剑情 发布于:2017-6-18 12:10 分类:Algorithms

正态分布标准公式

1111.png

μ代表均数,σ代表标准差。


Box-Muller算法生成符合正太分布随机数公式

2222.png


代码实现

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace BoxMullerTest
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void Form1_Load(object sender, EventArgs e)
  21. {
  22.  
  23. }
  24.  
  25. protected override void OnPaint(PaintEventArgs e)
  26. {
  27. float offsetX = 200;
  28. float offsetY = 0;
  29. float scaleX = 50;
  30. float scaleY = 700;
  31. //标准正态分布公式生成图像(红色)
  32. for (double x = -5; x <= 5; x += 0.05)
  33. {
  34. double y = NormalDistribution(x);
  35. PaintPoint(e, (float)x * scaleX + offsetX, (float)y * scaleY + offsetY);
  36. }
  37.  
  38. //BoxMuller算法生成图像(绿色)
  39. for (double i = 0; i < 100; i++ )
  40. {
  41. Point p = BoxMuller.RandPoint();
  42. PaintPoint1(e, (float)p.x * 20 + 180, (float)p.y * 10+ 50);
  43. }
  44. }
  45.  
  46. public void PaintPoint(PaintEventArgs e, float x, float y)
  47. {
  48. Graphics g = e.Graphics;
  49. Pen myPen = new Pen(Color.Red, 1);
  50. g.DrawEllipse(myPen, x, y, 3, 3);
  51. }
  52.  
  53. public void PaintPoint1(PaintEventArgs e, float x, float y)
  54. {
  55. Graphics g = e.Graphics;
  56. Pen myPen = new Pen(Color.Green, 1);
  57. g.DrawEllipse(myPen, x, y, 3, 3);
  58. }
  59.  
  60. /// <summary>
  61. /// 通过x值求正态分布的y值
  62. /// </summary>
  63. /// <param name="x">输入值</param>
  64. /// <param name="miu">均数</param>
  65. /// <param name="sigma">标准差</param>
  66. /// <returns></returns>
  67. public static double NormalDistribution(double x, double miu = 0, double sigma = 1)
  68. {
  69. //正态分布公式
  70. double y = 1.0 / Math.Sqrt(2 * Math.PI * sigma * sigma) / sigma * Math.Exp(-1 * (x - miu) * (x - miu) / (2 * sigma * sigma));
  71. return y;
  72. }
  73. }
  74.  
  75. public class Point
  76. {
  77. public double x;
  78. public double y;
  79.  
  80. public Point(double x, double y)
  81. {
  82. this.x = x;
  83. this.y = y;
  84. }
  85. }
  86.  
  87. /// <summary>
  88. /// Box-Muller算法是一种能根据均匀分布的随机数来产生正态分布的随机数的算法。
  89. /// 根据Box-Muller算法,假设a、b是两个服从均匀分布并且取值范围为从0到1的随机数,
  90. /// 我们就可以通过下面的公式获得两个满足正态分布(均数为0,标准差为1)的随机数x和y
  91. /// </summary>
  92. public class BoxMuller
  93. {
  94. static Random rd;
  95. static BoxMuller()
  96. {
  97. rd = new Random();
  98. }
  99.  
  100. /// <summary>
  101. /// 生成符合正态分布的随机数(均数为0,标准差为1)
  102. /// </summary>
  103. /// <returns></returns>
  104. public static Point RandPoint()
  105. {
  106. //生成两个随机数范围0到1
  107. double a = rd.NextDouble();
  108. double b = rd.NextDouble();
  109.  
  110. //在以半径为radius的圆上取随机角度的点
  111. double radius = Math.Sqrt(-2 * Math.Log(a));
  112. double angle = 2 * Math.PI * b;
  113. double z0 = angle * Math.Cos(angle);
  114. double z1 = angle * Math.Sin(angle);
  115.  
  116. Point p = new Point(z0, z1);
  117. return p;
  118. }
  119. }
  120. }

运行效果

3333.png

标签: Algorithms

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号