Non-repetitive random number

后端 未结 10 1885
囚心锁ツ
囚心锁ツ 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 08:54

    I did one this way awhile back. I don't know how it compares to the other methods presented as far as efficiency, randomness, etc. But it seems to work:

    List integers = new List() { 1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12 };
    
    Random rnd = new Random();
    
    var ints = from i in integers
               orderby rnd.Next(integers.Count)
               select i;
    

提交回复
热议问题