How can I generate a random number within a range but exclude some?

后端 未结 8 1144
忘掉有多难
忘掉有多难 2020-11-28 06:08

How can I generate a random number within a range but exclude some, without keep generating and checking if the generated number is one of those that I want to exclude?

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 06:51

    Depending on how large the list of random numbers you're excluding, I would just generate your numbers and check if it's in the array of excluded numbers- if it is, just discard it. I know you don't want to check every time, but I can't think of another way besides specifying the ranges explicitly and if you have more than 5 numbers you're excluding that might be a bit worse.

提交回复
热议问题