Non-repetitive random number

后端 未结 10 1913
囚心锁ツ
囚心锁ツ 2020-11-27 08:10

To generate Random numbers from 1- 20 I need to pick selective and it should not be repetitive.

How to do this in C#

Note I need to loop through as like this

10条回答
  •  温柔的废话
    2020-11-27 09:11

    class Program
    {
        static void Main(string[] args)
        {        
            List list = new List();
            int val;
            Random r;
            int IntialCount = 1;
            int count = 7 ;
            int maxRandomValue = 8;
    
            while (IntialCount <= count)
            {
                r = new Random();
                val = r.Next(maxRandomValue);
                if (!list.Contains(val))
                {
                    list.Add(val);
                    IntialCount++;
                }
    
            } 
        }
    }
    

提交回复
热议问题