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;
}
}
Copy Right By Robo_zou