惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
U
Unit 42
F
Fortinet All Blogs
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
Y
Y Combinator Blog
罗磊的独立博客
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
I
InfoQ
博客园 - 叶小钗
博客园 - 聂微东
Last Week in AI
Last Week in AI

博客园 - 凯锐

旋转的Silverlight文字 面试.NET高级软件工程师 C#利用批处理实现正在运行的程序自动更新 WindowsXp下安装Ubuntu8.10 英语学习者的十句经典名言 Crystal Report動態顯示圖片 基於IIS的WCF的分布式多層架構開發實現 ASP.net 網站和Web Application的區別(轉) Word2003中進行目錄排版 Asp.net動態添加控件 利用.NET代碼實現點擊按鈕彈出新窗體,輸入數據后返回並刷新頁面 SmartGridView(VB) C#/VB.NET语法的比较 我也来试一下利用word2007发布blog WinForm下多层架构的实现 從無到有實現一個xml數據庫登錄驗証 看完越獄20件必須懂的東西(轉) 在SQL 2000中交叉表的應用 DIY网站的方法 - 凯锐 - 博客园
產生指定個數的不重復隨機數
凯锐 · 2006-12-13 · via 博客园 - 凯锐

        隨機數以相等的概率從有限的數字集中選取,隨機數的產生始於種子值。如果重複使用相同的種子會產生相同的連續數字。也就說隨機數并非完全隨機﹐如果說我們的程序中需要使用不重復的隨機值該如何做呢?最近本人就碰到過這種情況﹐需要產生一組4位數的不重復隨機數字﹐首先想到的是使用.NET的random類別﹐可是實踐中使用來使用去﹐總是會產生几個重復的數字﹐甚至隔三差五的就會出來。效果極不理想。郁悶之中﹐本著咱DIY的精神﹐另外實現了一種隨機數的產生方法。
         實現思路﹕
                    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();
        }

    }
}

                   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[112]);
            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[43]);
            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[44]);
            Console.WriteLine(
"\n");

            Console.WriteLine(

"Press any key to continue");
            Console.ReadLine();
        }
    }
}


          

實現亮點﹕
                   真正隨機不重復﹑可指定輸出位數﹑也可隨機讀出﹐詳細請自行下載查閱代碼﹗