Generate N random and unique numbers within a rang

2019-01-05 05:30发布

What is an efficient way of generating N unique numbers within a given range using C#? For example, generate 6 unique numbers between 1 and 50. A lazy way would be to simply use Random.Next() in a loop and store that number in an array/list, then repeat and check if it already exists or not etc.. Is there a better way to generate a group of random, but unique, numbers? To add more context, I would like to select N random items from a collection, using their index.

thanks

7条回答
时光不老,我们不散
2楼-- · 2019-01-05 06:05

instead of using List use Dictionary!!

查看更多
登录 后发表回答