码迷,mamicode.com
首页 > Windows程序 > 详细

c# 随机数生成

时间:2019-12-24 20:37:02      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:ace   col   随机数   wing   orm   win   ash   bytearray   art   

 //定义随机数产生函数
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();
            }

        }
    }
}

c# 随机数生成

标签:ace   col   随机数   wing   orm   win   ash   bytearray   art   

原文地址:https://www.cnblogs.com/gougouwang/p/12093271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!