积极答复者
生成不重复随机数和固定数组乱序的问题

问题
-
1、固定数组乱序排列:
设数组包括1-999的所有整数,怎样将该数组乱序排列
2、生成指定数量的4位随机数,该数量少于9000(9999-1000+1)
private List<int> genRandomCode(int num)
{
Random ran = new Random();//实例一个随机器
List<int> list = new List<int>(num);//定义一个指定容量的链表
int i = 1;
while (i <= num)
{
int temp = ran.Next(1000, 10000)
if (!list.Contains(temp))//如果链表中不存在这个数
{
list.Add(temp);//链表中添加该数
i++;
}
}
return list;
}
这个方法总觉得不太可靠,
一是list是不是比较占内存,用数组或字典能不能好点
二是当要求生成的数量比较大时,random.next如果总是找到重复的值,该方法的结束时间是不是变得不可预测
答案
-
针对这类问题小弟都是用空间换时间的做法,请参考:
1、固定数组乱序排列:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<int> yourList = new List<int>(); //add number from 1 to 999 for (int i = 1; i <= 999; i++) { yourList.Add(i); } //打乱 shuffle(yourList); //顯示打乱結果 foreach (int item in yourList) { Console.WriteLine(item); } Console.ReadKey(); } // swaps array elements i and j public static void exch(List<int> list, int i, int j) { int swap = list[i]; list[i] = list[j]; list[j] = swap; } // take as input an array of strings and rearrange them in random order public static void shuffle(List<int> list) { int N = list.Count; Random rand=new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < N; i++) { int r = (rand.Next(0,N)); // between i and N-1 exch(list, i, r); } } } }
2、生成指定数量的4位随机数,该数量少于9000(9999-1000+1)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<int> yourList = new List<int>(); //add number from 1000 to 9999 for (int i = 1000; i <= 9999; i++) { yourList.Add(i); } //打乱 shuffle(yourList); //取得指定数量的4位随机数 List<int> newList = genRandomCode(yourList, 3); //show result foreach (int item in newList) { Console.WriteLine(item); } Console.ReadKey(); } // swaps array elements i and j public static void exch(List<int> list, int i, int j) { int swap = list[i]; list[i] = list[j]; list[j] = swap; } // take as input an array of strings and rearrange them in random order public static void shuffle(List<int> list) { int N = list.Count; Random rand=new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < N; i++) { int r = (rand.Next(0,N)); // between i and N-1 exch(list, i, r); } } //因为list内容早已打乱,所以随便取num项就好了 private static List<int> genRandomCode(List<int> list,int num) { List<int> newList = new List<int>(); for (int i = 0; i < num; i++) { newList.Add(list[i]); } return newList; } } }
- The blog of typewriter職人
- Convert C# to VB.NET
- /*If my concept is wrong ,please correct me.Thanks.*/
- 已建议为答案 ThankfulHeartModerator 2011年12月29日 5:08
- 已标记为答案 Lie YouModerator 2012年1月2日 2:53
-
补充回答(应该效率更高更快):
第一问——使用List<int>的Insert方法,动态随机插入:
List<int> numbers = new List<int>();
Random r = new Random(DateTime.Now.Millisecond);
for (int i = 1; i < 1000; i++)
{
numbers.Insert(r.Next(0, numbers.Count), i);
}第二问:可以先把1000~9999存储到List<int>中,然后随机取出一个,删除一个,总共取8999次(小于9000次):
//使用Enumerable(3.5)的扩展方法类生成1000~9999数字
List<int> numbers = new List<int>(Enumerable.Range(1000, 9999));
Random r = new Random(DateTime.Now.Millisecond);
//要取出的对应索引
int index = 0;
for (int i = 1; i < 9000; i++)
{
index = r.Next(0, numbers.Count);
//打印取出的数字
Console.WriteLine(numbers[index]);
//移除这个数字
numbers.RemoveAt(index);
}
QQ我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处- 已建议为答案 小歐ouMVP 2011年12月29日 3:44
- 已编辑 ThankfulHeartModerator 2011年12月29日 5:08
- 已标记为答案 Lie YouModerator 2012年1月2日 2:53
-
1、固定数组乱序排列:
设数组包括1-999的所有整数,怎样将该数组乱序排列
再补充
可以用Linq效率更快
List<int> listLinq = new List<int>(Enumerable.Range(1, 999)); var result = listLinq.OrderBy(o => Guid.NewGuid().GetHashCode());//最后不能.ToList也不能.ToArray(),否则效率会慢
- The blog of typewriter職人
- Convert C# to VB.NET
- /*If my concept is wrong ,please correct me.Thanks.*/
- 已建议为答案 ThankfulHeartModerator 2011年12月30日 8:37
- 已编辑 Shadow .Net 2011年12月30日 9:11
- 已标记为答案 Lie YouModerator 2012年1月2日 2:53
全部回复
-
针对这类问题小弟都是用空间换时间的做法,请参考:
1、固定数组乱序排列:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<int> yourList = new List<int>(); //add number from 1 to 999 for (int i = 1; i <= 999; i++) { yourList.Add(i); } //打乱 shuffle(yourList); //顯示打乱結果 foreach (int item in yourList) { Console.WriteLine(item); } Console.ReadKey(); } // swaps array elements i and j public static void exch(List<int> list, int i, int j) { int swap = list[i]; list[i] = list[j]; list[j] = swap; } // take as input an array of strings and rearrange them in random order public static void shuffle(List<int> list) { int N = list.Count; Random rand=new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < N; i++) { int r = (rand.Next(0,N)); // between i and N-1 exch(list, i, r); } } } }
2、生成指定数量的4位随机数,该数量少于9000(9999-1000+1)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<int> yourList = new List<int>(); //add number from 1000 to 9999 for (int i = 1000; i <= 9999; i++) { yourList.Add(i); } //打乱 shuffle(yourList); //取得指定数量的4位随机数 List<int> newList = genRandomCode(yourList, 3); //show result foreach (int item in newList) { Console.WriteLine(item); } Console.ReadKey(); } // swaps array elements i and j public static void exch(List<int> list, int i, int j) { int swap = list[i]; list[i] = list[j]; list[j] = swap; } // take as input an array of strings and rearrange them in random order public static void shuffle(List<int> list) { int N = list.Count; Random rand=new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < N; i++) { int r = (rand.Next(0,N)); // between i and N-1 exch(list, i, r); } } //因为list内容早已打乱,所以随便取num项就好了 private static List<int> genRandomCode(List<int> list,int num) { List<int> newList = new List<int>(); for (int i = 0; i < num; i++) { newList.Add(list[i]); } return newList; } } }
- The blog of typewriter職人
- Convert C# to VB.NET
- /*If my concept is wrong ,please correct me.Thanks.*/
- 已建议为答案 ThankfulHeartModerator 2011年12月29日 5:08
- 已标记为答案 Lie YouModerator 2012年1月2日 2:53
-
补充回答(应该效率更高更快):
第一问——使用List<int>的Insert方法,动态随机插入:
List<int> numbers = new List<int>();
Random r = new Random(DateTime.Now.Millisecond);
for (int i = 1; i < 1000; i++)
{
numbers.Insert(r.Next(0, numbers.Count), i);
}第二问:可以先把1000~9999存储到List<int>中,然后随机取出一个,删除一个,总共取8999次(小于9000次):
//使用Enumerable(3.5)的扩展方法类生成1000~9999数字
List<int> numbers = new List<int>(Enumerable.Range(1000, 9999));
Random r = new Random(DateTime.Now.Millisecond);
//要取出的对应索引
int index = 0;
for (int i = 1; i < 9000; i++)
{
index = r.Next(0, numbers.Count);
//打印取出的数字
Console.WriteLine(numbers[index]);
//移除这个数字
numbers.RemoveAt(index);
}
QQ我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处- 已建议为答案 小歐ouMVP 2011年12月29日 3:44
- 已编辑 ThankfulHeartModerator 2011年12月29日 5:08
- 已标记为答案 Lie YouModerator 2012年1月2日 2:53
-
1、固定数组乱序排列:
设数组包括1-999的所有整数,怎样将该数组乱序排列
再补充
可以用Linq效率更快
List<int> listLinq = new List<int>(Enumerable.Range(1, 999)); var result = listLinq.OrderBy(o => Guid.NewGuid().GetHashCode());//最后不能.ToList也不能.ToArray(),否则效率会慢
- The blog of typewriter職人
- Convert C# to VB.NET
- /*If my concept is wrong ,please correct me.Thanks.*/
- 已建议为答案 ThankfulHeartModerator 2011年12月30日 8:37
- 已编辑 Shadow .Net 2011年12月30日 9:11
- 已标记为答案 Lie YouModerator 2012年1月2日 2:53