方法一:
int[] intArr = new int[100]; 
ArrayList myList = new ArrayList();
Random rnd = new Random();
while (myList.Count < 100)
{
int num = rnd.Next(1, 101);
if (!myList.Contains(num))
myList.Add(num);
}
for (int i = 0; i <100; i++)
{
intArr[i] = (int)myList[i];
Console.Write("{0} ", intArr[i]);
Console.WriteLine();
}

方法二:
public int[] GetNoRepeatArrayy(int arrLength) 2 { 3 int[] array = new int[arrLength]; 4 IList<int> list = new List<int>(); 5 //准备不重复的数据 6 for (int i = 0; i < array.Length; i++) 7 { 8 list.Add(i); 9 } 10 //将不重复的数据随机插入到数组中 11 for (int j = (list.Count - 1); j > -1; j--) 12 { 13 //获得数据的随机索引 14 int index = new Random(Guid.NewGuid().GetHashCode()).Next(0, list.Count); 15 //给数组赋值 16 array[j] = list[index]; 17 //删除废弃数据,避免重复数据插入到数组中 18 list.RemoveAt(index); 19 } 20 return array; 21 }
posted on 2017-04-12 14:25  开心的柚子  阅读(326)  评论(0编辑  收藏  举报