Generate N random and unique numbers within a range

后端 未结 7 2005
不知归路
不知归路 2020-11-27 07:50

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

7条回答
  •  时光取名叫无心
    2020-11-27 08:09

    Take an array of 50 elements: {1, 2, 3, .... 50} Shuffle the array using any of the standard algorithms of randomly shuffling arrays. The first six elements of the modified array is what you are looking for. HTH

提交回复
热议问题