亲宝软件园·资讯

展开

c# 随机数生成

狗狗王 人气:0
 //定义随机数产生函数
static int GetRandomNumber(int min, int max)
        {
            int rtn = 0;
            Random r = new Random();
            byte[] buffer = Guid.NewGuid().ToByteArray();
            int iSeed = BitConverter.ToInt32(buffer, 0);
            r = new Random(iSeed);
            rtn = r.Next(min, max + 1);
            return rtn;
        }

//生成随机数
a[i] = GetRandomNumber(1, 33);  //产生1-33的随机数

//*************实例,双色球彩票随机数生成*******************//
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;

namespace SSQYH
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static int GetRandomNumber(int min, int max)
        {
            int rtn = 0;
            Random r = new Random();
            byte[] buffer = Guid.NewGuid().ToByteArray();
            int iSeed = BitConverter.ToInt32(buffer, 0);
            r = new Random(iSeed);
            rtn = r.Next(min, max + 1);
            return rtn;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            int i, j, k, l, b, x;
            int[] a = new int[6];
            for (i = 0; i < 6; i++)
            {
                //a[i] = Guid.NewGuid().GetHashCode() % 33;
                a[i] = GetRandomNumber(1, 33);                 //产生数据
                for (j = 0; j < i; j++)                                   //去重复
                    if (a[j] == a[i])
                        i = i - 1;
            }
            for (k = 0; k < 6; k++)                                          //排序               
                for (l = k + 1; l < 6; l++)
                    if (a[k] > a[l])
                    {
                        x = a[k];
                        a[k] = a[l];
                        a[l] = x;
                    }

            b = GetRandomNumber(1, 16);

            textBox1.Text = a[0].ToString();
            textBox2.Text = a[1].ToString();
            textBox3.Text = a[2].ToString();
            textBox4.Text = a[3].ToString();
            textBox5.Text = a[4].ToString();
            textBox6.Text = a[5].ToString();
            textBox7.Text = b.ToString();

        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            int i, j, k, l, b, c, x, y;
            int[] a = new int[5];
            for (i = 0; i < 5; i++)
            {
                //a[i] = Guid.NewGuid().GetHashCode() % 33;
                a[i] = GetRandomNumber(1, 35);                 //产生数据
                for (j = 0; j < i; j++)                                     //去重复
                    if (a[j] == a[i])
                        i = i - 1;
            }
            for (k = 0; k < 5; k++)                                          //排序               
                for (l = k + 1; l < 5; l++)
                    if (a[k] > a[l])
                    {
                        x = a[k];
                        a[k] = a[l];
                        a[l] = x;
                    }


            while (true)
            {
                b = GetRandomNumber(1, 12);
                c = GetRandomNumber(1, 12);
                if (b != c)
                {
                    if (b > c)
                    {
                        y = b;
                        b = c;
                        c = y;
                    }

                    textBox13.Text = b.ToString();
                    textBox14.Text = c.ToString();

                    break;
                }

            }


            textBox8.Text = a[0].ToString();
            textBox9.Text = a[1].ToString();
            textBox10.Text = a[2].ToString();
            textBox11.Text = a[3].ToString();
            textBox12.Text = a[4].ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int i, j, k, l, b, c, x, y;
            int[] a = new int[7];
            for (i = 0; i < 7; i++)
            {
                //a[i] = Guid.NewGuid().GetHashCode() % 33;
                a[i] = GetRandomNumber(1, 33);                 //产生数据
                for (j = 0; j < i; j++)                                     //去重复
                    if (a[j] == a[i])
                        i = i - 1;
            }
            for (k = 0; k < 7; k++)                                          //排序               
                for (l = k + 1; l < 7; l++)
                    if (a[k] > a[l])
                    {
                        x = a[k];
                        a[k] = a[l];
                        a[l] = x;
                    }

            textBox15.Text = a[0].ToString();
            textBox16.Text = a[1].ToString();
            textBox17.Text = a[2].ToString();
            textBox18.Text = a[3].ToString();
            textBox19.Text = a[4].ToString();
            textBox20.Text = a[5].ToString();
            textBox21.Text = a[6].ToString();

            while (true)
            {
                b = GetRandomNumber(1, 16);
                c = GetRandomNumber(1, 16);
                if (b != c)
                {
                    if (b > c)
                    {
                        y = b;
                        b = c;
                        c = y;
                    }
                }
                textBox22.Text = b.ToString();
                textBox23.Text = c.ToString();
            }

        }
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论