最近常用的一个算法(生成不重复的随机数组)

在数组规模不大的情况下,空间和时间损耗都不错。下面是源代码:
1 public static class Helper
2 {
3 #region 随机数生成
4 /// <summary>
5 /// 表示全局的伪随机数生成器。
6 /// </summary>
7 public static readonly Random GlobalRandom = new Random();
8
9 /// <summary>
10 /// 生成不重复随机数组。
11 /// </summary>
12 /// <param name="array"></param>
13 public static void RandomSwap(int[] array)
14 {
15 int count = array.Length;
16
17 for (int i = 0; i < count; i++)
18 {
19 int index1 = GlobalRandom.Next(0, count - i);
20 int index2 = count - i - 1;
21
22 int temp = array[index1];
23 array[index1] = array[index2];
24 array[index2] = temp;
25 }
26 }
27 #endregion
28 }
posted @ 2011-03-23 14:28  Angel Lucifer  阅读(985)  评论(1编辑  收藏  举报