TestRandom

 

本人数学不好

想用计算机测试下概率

就是设置一个目标数字(如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;

            }

        }

 

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值