Can Python generate a random number that excludes a set of numbers, without using recursion?

前端 未结 7 1368
悲哀的现实
悲哀的现实 2020-12-30 20:46

I looked over Python Docs (I may have misunderstood), but I didn\'t see that there was a way to do this (look below) without calling a recursive function.
What I\'d like

7条回答
  •  感动是毒
    2020-12-30 21:10

    Use random.choice(). In this example, a is your lower bound, the range between b and c is skipped and d is your upper bound.

    import random
    numbers = range(a,b) + range(c,d)
    r = random.choice(numbers)
    

提交回复
热议问题