隨機數以相等的概率從有限的數字集中選取,隨機數的產生始於種子值。如果重複使用相同的種子會產生相同的連續數字。也就說隨機數并非完全隨機﹐如果說我們的程序中需要使用不重復的隨機值該如何做呢?最近本人就碰到過這種情況﹐需要產生一組4位數的不重復隨機數字﹐首先想到的是使用.NET的random類別﹐可是實踐中使用來使用去﹐總是會產生几個重復的數字﹐甚至隔三差五的就會出來。效果極不理想。郁悶之中﹐本著咱DIY的精神﹐另外實現了一種隨機數的產生方法。
實現思路﹕
1﹑首先定義一個定長的一維數組,并以0至定長長度賦初始值給數組。
2﹑遍歷該數組﹐首先隨機產生一個數字﹐假設為K,將1中產生的數組中順序號為K的值 與當前順序號的值對換。
3﹑再次遍歷該數組﹐將其中的值以字符串的開式顯示出來。
實現步驟﹕
1﹑新增一個類別庫專案"KingNaRandom",將Visual Studio自動產生的Class1.cs改名﹕KingNaRand.cs。輸入以下代碼﹕
2﹑新增一主控台應用程式﹐名稱不改了﹐在Program.cs的Main方法中輸入以下代碼﹕
實現亮點﹕
真正隨機不重復﹑可指定輸出位數﹑也可隨機讀出﹐詳細請自行下載查閱代碼﹗
實現思路﹕
1﹑首先定義一個定長的一維數組,并以0至定長長度賦初始值給數組。
2﹑遍歷該數組﹐首先隨機產生一個數字﹐假設為K,將1中產生的數組中順序號為K的值 與當前順序號的值對換。
3﹑再次遍歷該數組﹐將其中的值以字符串的開式顯示出來。
實現步驟﹕
1﹑新增一個類別庫專案"KingNaRandom",將Visual Studio自動產生的Class1.cs改名﹕KingNaRand.cs。輸入以下代碼﹕
namespace KingNaRandom
{
/***********************************************************************************
****Component Name : KingNaRand
****Author: KingNa
****Create Date : 2006-12-14
****CopyRight: Reserve this info if you want to Use this Componet
***********************************************************************************/
using System;
public class KingNaRand
{
private int[] arr;
private int iLen;
public int iCount
{
set
{
if (value > 1) { this.iLen = value; }
}
}
public int this[int index]
{
get
{
if (index <= this.iLen) { return this.arr[index]; }
return -1;
}
}
public string this[int index,int iCount]
{
get
{
if(index<=this.iLen)
return this.arr[index].ToString().PadLeft(iCount, '0');
return index.ToString() + " is out of the len";
}
}
public KingNaRand()
{
this.iLen = 10;
}
/// <summary>
/// set the default randomize number is 10.
/// </summary>
/// <param name="iCount">set the number of randomize</param>
public KingNaRand(int iCount)
{
this.iLen = 10;
if (iCount > 1) { this.iLen = iCount; }
}
/// <summary>
/// Change the Number to String and Split it by symbol of ","
/// </summary>
public string List()
{
string strList = "";
for (int i = 0; i < (this.arr.Length - 1); i++)
{
strList = strList + this.arr[i] + ",";
}
return (strList + this.arr[this.arr.Length - 1]);
}
/// <summary>
/// Change the Number to String and Split it by symbol of ","
/// </summary>
/// <param name="iCount">set the number length</param>
/// <returns></returns>
public string List(int iCount)
{
string strList = "";
for (int i = 0; i < (this.arr.Length - 1); i++)
{
strList = strList + (this.arr[i].ToString().PadLeft(iCount,'0')) + ",";
}
return (strList + (this.arr[this.arr.Length - 1].ToString().PadLeft(iCount,'0')));
}
/// <summary>
/// Get the randomize number,the default number length is 10
/// </summary>
public void Next()
{
this.arr = new int[this.iLen];
Random rand = new Random();
for (int i = 0; i < this.arr.Length; i++)
{
this.arr[i] = i;
//this.arr[i] = rand.Next(10);
}
for (int j = 0; j < this.arr.Length; j++)
{
int k = rand.Next(this.arr.Length);
int m = this.arr[k];
this.arr[k] = this.arr[j];
this.arr[j] = m;
}
}
/// <summary>
/// Get the randomize number,the length was defined by user
/// </summary>
/// <param name="iCount">the length</param>
public void Next(int iCount)
{
if (iCount > 1) { this.iLen = iCount; }
this.Next();
}
}
}
{
/***********************************************************************************
****Component Name : KingNaRand
****Author: KingNa
****Create Date : 2006-12-14
****CopyRight: Reserve this info if you want to Use this Componet
***********************************************************************************/
using System;
public class KingNaRand
{
private int[] arr;
private int iLen;
public int iCount
{
set
{
if (value > 1) { this.iLen = value; }
}
}
public int this[int index]
{
get
{
if (index <= this.iLen) { return this.arr[index]; }
return -1;
}
}
public string this[int index,int iCount]
{
get
{
if(index<=this.iLen)
return this.arr[index].ToString().PadLeft(iCount, '0');
return index.ToString() + " is out of the len";
}
}
public KingNaRand()
{
this.iLen = 10;
}
/// <summary>
/// set the default randomize number is 10.
/// </summary>
/// <param name="iCount">set the number of randomize</param>
public KingNaRand(int iCount)
{
this.iLen = 10;
if (iCount > 1) { this.iLen = iCount; }
}
/// <summary>
/// Change the Number to String and Split it by symbol of ","
/// </summary>
public string List()
{
string strList = "";
for (int i = 0; i < (this.arr.Length - 1); i++)
{
strList = strList + this.arr[i] + ",";
}
return (strList + this.arr[this.arr.Length - 1]);
}
/// <summary>
/// Change the Number to String and Split it by symbol of ","
/// </summary>
/// <param name="iCount">set the number length</param>
/// <returns></returns>
public string List(int iCount)
{
string strList = "";
for (int i = 0; i < (this.arr.Length - 1); i++)
{
strList = strList + (this.arr[i].ToString().PadLeft(iCount,'0')) + ",";
}
return (strList + (this.arr[this.arr.Length - 1].ToString().PadLeft(iCount,'0')));
}
/// <summary>
/// Get the randomize number,the default number length is 10
/// </summary>
public void Next()
{
this.arr = new int[this.iLen];
Random rand = new Random();
for (int i = 0; i < this.arr.Length; i++)
{
this.arr[i] = i;
//this.arr[i] = rand.Next(10);
}
for (int j = 0; j < this.arr.Length; j++)
{
int k = rand.Next(this.arr.Length);
int m = this.arr[k];
this.arr[k] = this.arr[j];
this.arr[j] = m;
}
}
/// <summary>
/// Get the randomize number,the length was defined by user
/// </summary>
/// <param name="iCount">the length</param>
public void Next(int iCount)
{
if (iCount > 1) { this.iLen = iCount; }
this.Next();
}
}
}
2﹑新增一主控台應用程式﹐名稱不改了﹐在Program.cs的Main方法中輸入以下代碼﹕
namespace ConsoleApplication1
{
using System;
using KingNaRandom;
class Program
{
static void Main(string[] args)
{
KingNaRand knr = new KingNaRand();
knr.Next();
Console.WriteLine(knr.List());
Console.WriteLine(knr.List(2));
Console.WriteLine("The 5th number: {0}", knr[4].ToString());
Console.WriteLine("The 5th number: {0}", knr[11, 2]);
Console.WriteLine("\n");
knr.Next(100);
Console.WriteLine(knr.List());
Console.WriteLine(knr.List(3));
Console.WriteLine("The 5th number: {0}", knr[4].ToString());
Console.WriteLine("The 5th number: {0}", knr[4, 3]);
Console.WriteLine("\n");
knr.iCount = 1000;
knr.Next();
Console.WriteLine(knr.List());
Console.WriteLine(knr.List(4));
Console.WriteLine("The 5th number: {0}", knr[4].ToString());
Console.WriteLine("The 5th number: {0}", knr[4, 4]);
Console.WriteLine("\n");
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
}
}
{
using System;
using KingNaRandom;
class Program
{
static void Main(string[] args)
{
KingNaRand knr = new KingNaRand();
knr.Next();
Console.WriteLine(knr.List());
Console.WriteLine(knr.List(2));
Console.WriteLine("The 5th number: {0}", knr[4].ToString());
Console.WriteLine("The 5th number: {0}", knr[11, 2]);
Console.WriteLine("\n");
knr.Next(100);
Console.WriteLine(knr.List());
Console.WriteLine(knr.List(3));
Console.WriteLine("The 5th number: {0}", knr[4].ToString());
Console.WriteLine("The 5th number: {0}", knr[4, 3]);
Console.WriteLine("\n");
knr.iCount = 1000;
knr.Next();
Console.WriteLine(knr.List());
Console.WriteLine(knr.List(4));
Console.WriteLine("The 5th number: {0}", knr[4].ToString());
Console.WriteLine("The 5th number: {0}", knr[4, 4]);
Console.WriteLine("\n");
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
}
}
實現亮點﹕
真正隨機不重復﹑可指定輸出位數﹑也可隨機讀出﹐詳細請自行下載查閱代碼﹗