How can I efficiently select several unique random numbers from 1 to 50, excluding x?

后端 未结 5 471
囚心锁ツ
囚心锁ツ 2021-01-06 19:01

I have 2 numbers which are between 0 and 49. Let\'s call them x and y. Now I want to get a couple of other numbers which are not x or y, but are al

5条回答
  •  感情败类
    2021-01-06 19:15

    You can use something called the Fisher-Yates shuffle. It's an efficient algorithm for producing a randomly ordered list of values from some set. You would first exclude N from the list of values from which to get random values, and then perform the shuffle.

提交回复
热议问题