本人近来面试碰到一题:产生100个不重复的随机数,解法如下

 

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        ArrayList array = MyArray.GetArray(10);
        foreach (int i in array)
        {
            Console.WriteLine(i);
        }
    }
}
class MyArray
{
    public static ArrayList GetArray(int length)
    {
        ArrayList myList = new ArrayList();
        Random rnd=new Random ();
        while (myList.Count < length)
        {
            int number = rnd.Next(1, length + 1);
            if(!myList.Contains(number))
            {
                myList.Add(number);
            }
        }
        return myList;
    }
}

 

C#下面产生不重复的随机数字

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-09-21
  • 2022-01-26
  • 2022-12-23
  • 2022-01-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-24
  • 2021-09-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案