可行的一种算法: 使用System.Collections.ObjectModel.Collection类做唯一性判别理论上比自己写一个排序查找算法要快一点,大概做法如下 System.Collections.ObjectModel.Collection<int> col = new System.Collections.ObjectModel.Collection<int>(); for(int i=0;i<qty;i++){ //取随机数找到一个值iValue
鉴于几百或几千相对于90万还是很小,所以可以简单处理: public static int[] GetRandomNumber(int min, int max, int count){ var list = new HashSet<int>(); Random r= new Random(); while(list.Count<count){ int n = r.Next(min, max); list.Add(n); } return list.ToArray(); }
鉴于几百或几千相对于90万还是很小,所以可以简单处理: public static int[] GetRandomNumber(int min, int max, int count){ var list = new HashSet<int>(); Random r= new Random(); while(list.Count<count){ int n = r.Next(min, max); list.Add(n); } return list.ToArray(); }
鉴于几百或几千相对于90万还是很小,所以可以简单处理: public static int[] GetRandomNumber(int min, int max, int count){ var list = new HashSet<int>(); Random r= new Random(); while(list.Count<count){ int n = r.Next(min, max); list.Add(n); } return list.ToArray(); }
2、所谓"取完后重新开始取",如果上一批的最后一个数是111111,而下一批的第一个随机数恰巧是111111,你又如何面对?
使用System.Collections.ObjectModel.Collection类做唯一性判别理论上比自己写一个排序查找算法要快一点,大概做法如下
System.Collections.ObjectModel.Collection<int> col = new System.Collections.ObjectModel.Collection<int>();
for(int i=0;i<qty;i++){
//取随机数找到一个值iValue
if(!col.Contains(iValue)){
i--;
}
}
可使用System.Collections.ObjectModel.Collection类Contains方法判断是否取重复了
var list = new HashSet<int>();
Random r= new Random();
while(list.Count<count){
int n = r.Next(min, max);
list.Add(n);
}
return list.ToArray();
}
var list = new HashSet<int>();
Random r= new Random();
while(list.Count<count){
int n = r.Next(min, max);
list.Add(n);
}
return list.ToArray();
}
var list = new HashSet<int>();
Random r= new Random();
while(list.Count<count){
int n = r.Next(min, max);
list.Add(n);
}
return list.ToArray();
}