C#下面产生不重复的随机数字

本人近来面试碰到一题:产生100个不重复的随机数,解法如下

 

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        ArrayList array = MyArray.GetArray(10);
        foreach (int i in array)
        {
            Console.WriteLine(i);
        }
    }
}
class MyArray
{
    public static ArrayList GetArray(int length)
    {
        ArrayList myList = new ArrayList();
        Random rnd=new Random ();
        while (myList.Count < length)
        {
            int number = rnd.Next(1, length + 1);
            if(!myList.Contains(number))
            {
                myList.Add(number);
            }
        }
        return myList;
    }
}

 

posted @ 2009-05-30 21:22  平平  阅读(798)  评论(4编辑  收藏  举报