Non-repetitive random number

后端 未结 10 1930
囚心锁ツ
囚心锁ツ 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:09

    Just like i did:

        list.Clear();
        int count = 0;
        
        while (count < 20)
        {            
            int x = Random.Range(1, 21);
            if (!list.Contains(x))
            {
                list.Add(x);
                count++;
            }
        }
    

提交回复
热议问题