How do I create a list of random numbers without duplicates?

前端 未结 17 2479
灰色年华
灰色年华 2020-11-22 13:30

I tried using random.randint(0, 100), but some numbers were the same. Is there a method/module to create a list unique random numbers?

Note: The fol

17条回答
  •  被撕碎了的回忆
    2020-11-22 13:31

    import random
    
    sourcelist=[]
    resultlist=[]
    
    for x in range(100):
        sourcelist.append(x)
    
    for y in sourcelist:
        resultlist.insert(random.randint(0,len(resultlist)),y)
    
    print (resultlist)
    

提交回复
热议问题