Задача: Разыграть 500 сл. в. в соответствии с мат. ожиданием m=4 и ср. кв. откл. sigma = 1.
Реализация:
const int mu = 4; const int sigma = 1; const int count = 500; int[] c = new int[30];
private void button1_Click(object sender, EventArgs e)
{
double[] x = new double[count];
double[] phi = new double[count];
double[] etta = new double[count];
double h = 0.01, eps = 0.001;
Random r = new Random();
for (int i=0;i<count;i++)
{
phi[i] = r.NextDouble() - 0.5;
}
double sum = 0;
int k = 1, stop = 0;
for (int i = 0; i < count; i++)
{
sum = (h / Math.Sqrt(2 * 3.1428)) * Math.Exp(-0.5 * h * h);
rep:
if (Math.Abs(phi[i]) - sum > eps)
{
sum += (Math.Abs(k * h - (k - 1) * h) / Math.Sqrt(2 * 3.1415)) * Math.Exp(-0.5 * k * h * k * h);
k++;
goto rep;
}
else
{
if (phi[i] > 0)
etta[i] = sigma * k * h + mu;
else
etta[i] = -1 * sigma * k * h + mu;
k = 1;
}
}
for (int i=0;i<count;i++)
{
richTextBox1.Text += Convert.ToString(etta[i]) + "\n";
}
for (int i = 0; i < count; i++)
{
for (int j = -15; j < 15; j++)
{
if (etta[i] < j)
{
c[j + 15]++; //массив счетчиков попадания
break;
}
}
}
for (int i = 0; i < c.Length; i++)
{
richTextBox2.Text += Convert.ToString(c[i]) + "\n";
}
for (int i=-15;i<15;i++)
{
chart1.Series[0].Points.AddXY(i, c[i+15]);
}
Комментарии к исходному коду:
1. Разыгрываем сл. в. с помощью класса Random и вычитаем 1/2
2. Составляем беск. сумму (считаем интеграл)
3. Приводим преобразования для нужных нам параметров распределения
4. Дробим интервал на несколько единичных (конечно, нужно взять побольше для показательной картины)
5. Строим гистограмму и выводим на chart1
2 способ:
const int mat_ozhid = 4; const int sigma = 1; const int count = 500; double[] ksi = new double[500]; int[] c = new int[30];
private void button1_Click(object sender, EventArgs e)
{
double sum=0;
Random r = new Random();
for (int i=0;i<count;i++)
{
for (int j=1;j<=12;j++)
{
sum+=r.NextDouble();
}
ksi[i] = sigma * (sum - 6) + mat_ozhid; richTextBox1.Text += Convert.ToString(ksi[i]) + "\n";
sum = 0;
}
for (int i = 0; i < count; i++)
{
for (int j = -15; j < 15; j++)
{
if (ksi[i] < j)
{
c[j + 15]++; //массив счетчиков попадания
break;
}
}
}
for (int i = 0; i < c.Length; i++)
{
richTextBox2.Text += Convert.ToString(c[i]) + "\n";
}
for (int i = -15; i < 15; i++)
{
chart1.Series[0].Points.AddXY(i, c[i + 15]);
}
}3 способ:
const int mat_ozhid = 4; const int sigma = 1; const int count = 500; int[] c = new int[20]; int count1 = 0;
double[] x = new double[count]; double[] etta = new double[count]; double etta1, g1, g2, ksi, help; double G = Math.Sqrt(Math.PI * 0.5);
private void button1_Click(object sender, EventArgs e)
{
int n=0;
Random r = new Random();
for (int i=0;i<2*count;i++)
{
g1 = r.NextDouble();
ksi = Math.Tan(Math.PI * (g1 - 0.5));
g2 = r.NextDouble();
etta1 = (g2 * G / Math.PI) * Math.Sin(Math.PI * g1) * Math.Sin(Math.PI * g1);
help = Math.Exp(-0.5 * ksi * ksi) / Math.Sqrt(2 * Math.PI);
if (etta1<=help)
{
count1++;
etta[n] = sigma * ksi + mat_ozhid;
n++;
if (count1 == 500)
goto end;
}
}
end:
for (int i = 0; i < count; i++)
{
for (int j = -12; j < 8; j++)
{
if (etta[i] < j)
{
c[j + 12]++; //массив счетчиков попадания
break;
}
}
}
for (int i=0;i<count;i++)
{
richTextBox1.Text += Convert.ToString(etta[i]) + "\n";
}
for (int i = 0; i < c.Length; i++)
{
richTextBox2.Text += Convert.ToString(c[i]) + "\n";
}
for (int i = -12; i < 8; i++)
{
chart1.Series[0].Points.AddXY(i, c[i + 12]);
}
