Python- How to generate random integers with multiple ranges?

后端 未结 6 1981
忘掉有多难
忘掉有多难 2021-01-12 01:25

I\'ve run into confusion in generating X amount of random integers from different sets of (a,b). For example, I would like to generate 5 random integers coming from (1,5), (

6条回答
  •  攒了一身酷
    2021-01-12 01:39

    for non repeated numbers:

    from random import randint, choice
    
    randoms = []
    counter = 0    
    while True:
       new_random = (choice([randint(1,5),randint(9,15),randint(21,27)]))
       if new_random not in randoms:
          randoms.append(new_random)
          counter += 1
       if counter == 5 :
          break
    print randoms
    

提交回复
热议问题