随机数的使用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace 摇奖机
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "抽奖机";
            button1.Text = "开始";
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(b == false)
            {
                b = true;
                button1.Text = "停止";
                Thread th = new Thread(PlayGame);
                th.IsBackground = true;
                th.Start();
                //PlayGame();
            }
            else
            {
                b = false;
                button1.Text = "开始";
            }

        }

        bool b = false;
        private void PlayGame()
        {
            //随机数的使用
            Random r = new Random();
            while(b)
            {
                //在[0-9]整数
                label1.Text = r.Next(0, 10).ToString();
                label2.Text = r.Next(0, 10).ToString();
                label3.Text = r.Next(0, 10).ToString();
                Thread.Sleep(100);
            }
        }
    }
}

  

posted @ 2017-08-25 17:02  mCat  Views(251)  Comments(0Edit  收藏  举报