不重复的随机数JAVA、C#·适合初级人员玩耍。
計(jì)算速度較快,10000個(gè)不重復(fù)的隨機(jī)數(shù)一秒內(nèi)肯定能出來,只要數(shù)據(jù)給的不過分,都行
JAVA語(yǔ)言
public static void main(String[] args) {
?? ??? ?Random ra = new Random();
?? ??? ?List<Integer> list=new ArrayList<Integer>();
?? ??? ?//范圍1-
?? ??? ?int scope=10;
?? ??? ?//需求長(zhǎng)度
?? ??? ?int count=10;
?? ??? ?if(count>scope){
?? ??? ??? ?System.exit(0);
?? ??? ?}
?? ??? ?for (;;) {
?? ??? ??? ?int ran=ra.nextInt(scope)+1;
?? ??? ??? ?if(!list.contains(ran)){
?? ??? ??? ??? ?list.add(ran);
?? ??? ??? ??? ?if(list.size()==count){
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?for (Integer integer : list) {
?? ??? ??? ?System.out.print(integer+",");
?? ??? ?}
?? ?}
?
?
C#語(yǔ)言
?static void Main(string[] args)
? ? ? ? {
?? ??? ??? ?Random ra = new Random();
?? ??? ??? ?List<Int32> list = new List<Int32>();
?? ??? ??? ?//范圍1-
?? ??? ??? ?int scope = 10;
?? ??? ??? ?//需求長(zhǎng)度
?? ??? ??? ?int count = 10;
?? ??? ??? ?if (count > scope)
?? ??? ??? ?{
?? ??? ??? ??? ?return;
?? ??? ??? ?}
?? ??? ??? ?for (; ; )
?? ??? ??? ?{
?? ??? ??? ??? ?int ran = ra.Next(scope) + 1;
?? ??? ??? ??? ?if (!list.Contains(ran))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?list.Add(ran);
?? ??? ??? ??? ??? ?if (list.Count() == count)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
? ? ? ? ? ? foreach (var item in list)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write(item+",");
? ? ? ? ? ? }
?? ??? ?}
總結(jié)
以上是生活随笔為你收集整理的不重复的随机数JAVA、C#·适合初级人员玩耍。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#红包波动系数·高可用性,适合企业级开
- 下一篇: 56个民族sql语句