一个简单的C#双色球号码随机生成器

来源:互联网 发布:网络小本创业 编辑:程序博客网 时间:2024/04/16 20:35

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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private bool timeStart = false;      //标记时间控件是否开始

        private const int redNum = 6;         //红球数目
        private const int redMaxValue = 33;   //红球最大数
        private const int redMinValue = 1;    //红球最小数

        private const int blueNum = 1;        //蓝球数目
        private const int blueMaxValue = 16;  //蓝球最大数
        private const int blueMinValue = 1;   //蓝球最小数

        public Form1()
        {
            InitializeComponent();
        }

        public int[] sort(int[] num)
        {
            int i,j ,temp;
            int n = num.Length;
            for (i = 0; i < n - 1; i++)
            {
                for (j = i + 1; j < n; j++) /*注意循环的上下限*/
                {
                    if (num[i] > num[j])
                    {
                        temp = num[i];
                        num[i] = num[j];
                        num[j] = temp;
                    }
                }
            }
            return num;
        }

        public int getNum(int[] arrNum, int tmp, int minValue, int maxValue, Random ra)
        {
            int n = 0;
            while (n <= arrNum.Length - 1)
            {
                if (arrNum[n] == tmp) //利用循环判断是否有重复
                {
                    tmp = ra.Next(minValue, maxValue); //重新随机获取。
                    getNum(arrNum, tmp, minValue, maxValue, ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。
                }
                n++;
            }
            return tmp;
        }

        public string getRandomNum(int num, int minValue, int maxValue)
        {
            Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
            string strNum = "";
            int[] arrNum = new int[num];
            int tmp = 0;

            for (int i = 0; i <= num - 1; i++)
            {
                tmp = ra.Next(minValue, maxValue); //随机取数
                arrNum[i] = getNum(arrNum, tmp, minValue, maxValue, ra); //取出值赋到数组中
            }

            this.sort(arrNum);

            for (int i = 0; i <arrNum.Length; i++)
            {

                string s = Convert.ToString(arrNum[i]);

                //如果产生的数字为1位数,
                //前面补“0”
                if (s.Length == 1)
                {
                    s = s.PadLeft(2,'0');
                }               
                strNum = strNum + s + " ";
            }           
            return strNum;
        }


        private void btnCreate_Click(object sender, EventArgs e)
        {

            if (timeStart)
            {
                this.btnCreate.Text = "生成号码";
                timer1.Stop();
                timeStart = false;
            }
            else
            {
                this.btnCreate.Text = "停止";
                timer1.Start();
                timeStart = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.btnCreate.Text = "生成号码";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //产生6个红球号码               
            this.txtRedNumber.Text = this.getRandomNum(redNum, redMinValue, redMaxValue).Trim();

            //产生1个蓝球号码
            this.txtBlue.Text = this.getRandomNum(blueNum,blueMinValue,blueMaxValue);
        }

        private void tbPace_Scroll(object sender, EventArgs e)
        {
            //移动滚动条的位置以设定产生新数据的速度

            int i = this.tbPace.Value;
            this.timer1.Interval = 1001-i * 100;
        }
    }
}

原创粉丝点击