随机函数
System.Random rnd = new System.Random();
myValue=rnd.Next(1, 100); //产生1-100之间的一个随机数
//以下代码实现产生1-54之间不重复的数列
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Random
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
intWindow();
Console.WriteLine("this is testing");
RandomExample();
}
private void intWindow()
{
this.Text = "Random Number Example";
}
private void RandomExample()
{
System.Random rnd = new System.Random();
int[] myNumber=new int[55];
int myValue1,myValue2,k1,k2;
for (int i = 1; i <= 54; i++)
{
myNumber[i] = i;
}
for (int i = 1; i < 100; i++)
{
myValue1 = rnd.Next(1, 55);
myValue2 = rnd.Next(1, 55);
k1 = myNumber[myValue1];
k2 = myNumber[myValue2];
myNumber[myValue1] = k2;
myNumber[myValue2] = k1;
}
for (int i = 1; i <= 54; i++)
{
if (this.textBox1.Text == "")
{
this.textBox1.Text = myNumber[i].ToString();
}
else
{
this.textBox1.Text = this.textBox1.Text + "," + myNumber[i].ToString();
}
}
}
}
}