Non-repeated random numbers JAVA, C#

The calculation speed is relatively fast. 10,000 non-repeated random numbers will definitely come out within one second. As long as the data is not given too much, it will do.

JAVA language

public static void main(String[] args) {
        Random ra = new Random();
        List<Integer> list=new ArrayList<Integer>();
        //范围1-
        int scope=10;
        //需求长度
        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# language

 static void Main(string[] args)
        {
            Random ra = new Random();
            List<Int32> list = new List<Int32>();
            //范围1-
            int scope = 10;
            //需求长度
            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+",");
            }
        }

Guess you like

Origin blog.csdn.net/feng8403000/article/details/106495426