samedi 28 septembre 2019

How to generate unique random numbers from a list

I know that I should write a code to random numbers like the code below, but I don't know how to generate unique numbers, for example I want to generate 3 unique phone numbers from a list at once, how can I do it?

import random  

# last numbers of the phone numbers are in order, 1,2,3,4,5,6
myList = [60165651, 60175652, 60156523, 60135354, 60159535, 60192326]  

for x in range(5): 
    print(random.choice(myList))

-----------------------------------------------------

another problem is that if I put zero at the beginning of numbers I get an error: Token invalid!

e.g:

myPhoneList = [060165651, 60175652, 60156523, 60135354, 60159535, 60192326]



Aucun commentaire:

Enregistrer un commentaire