本人数学不好
想用计算机测试下概率
就是设置一个目标数字(如100)
用计算机的随机数以目标数字(如100)产生随机数(0-100之间)
当产生的随机数大于或等于目标数(如>=100)时候设置变量TureCount++,
当产生的随机数小于于目标数(如<100)时候 设置变量FalseCount++,
然后统计(TureCount-FalseCount)的值大于或等于某个值(如Abs(TureCount-FalseCount)>=10)的情况(出现的次数)!
发现有点不可思议!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int TureCount;
int FalseCount;
int i, j, result;
private void button1_Click(object sender, EventArgs e)
{
if (timer1.Enabled)
{
button1.Text = "Start";
timer1.Stop();
}
else {
button1.Text = "Stop";
timer1.Start();
}
}
void test(int input) {
i++;
Random rnd = new Random();
int r = rnd.Next(input);
if (r >= (input / 2))
{
TureCount++;
label1.Text = "TrueCount:" + TureCount.ToString();
}
else {
FalseCount++;
label2.Text = "FalseCount:" + FalseCount.ToString();
}
label3.Text = "Time--- " + i.ToString() + "---CurrentRandom: " + r.ToString();
if (Math.Abs((TureCount - FalseCount)) > numericUpDown1.Value)
{
result++;
Lbresult.Text = result.ToString() ;
// timer1.Stop();
//return;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
test(Convert.ToInt32(numericUpDown2.Value));
}
private void button2_Click(object sender, EventArgs e)
{
reset();
}
void reset(){
timer1.Stop();
button1.Text = "Start";
i = 0;
TureCount = 0;
FalseCount = 0;
result = 0;
label1.Text = "TrueCount:" + TureCount.ToString();
label2.Text = "FalseCount:" + TureCount.ToString();
label3.Text = "Time--- " + i.ToString() + "---CurrentRandom: None";
Lbresult.Text = "0";
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
if( numericUpDown2.Value>=Int32.MaxValue){
numericUpDown2.Value=Int32.MaxValue;
}
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown1.Value >= Int32.MaxValue)
{
numericUpDown1.Value = Int32.MaxValue;
}
}
}
}