samedi 21 mars 2015

Python: generating random number with different digits

Hello guys:)


I have some questions, I hope you all could help me...at least a bit.


So I need to write a program which generates random numbers from (100-999), and the tricky part is, that the digits in the number can't be the same.


For example: 222, 212 and so on.


So far, I have this:



import random

a = int (random.randint(1,9)) <-- first digit
b = int (random.randint(0,9)) <-- second digit
c = int (random.randint(0,9)) <-- third digit

if (a != b and a != b and b != c):
print a,b,c


So yeah, as you can see, I generate all three digits separately. I think it's easier to check if there are same digits in the number.


So, now I want to do a loop that will generate the numbers until the requirements are met (now it either prints blank page or the number). Not It generates only once and I have to open the program again.


In C++ I did that with the 'while loop '. And here I don't know how to do it.


And one more question. How can I code the number(quantity) of random numbers I want to generate? To be more specific: I want to generate 4 random numbers. How should I code it?


Thank you.


Aucun commentaire:

Enregistrer un commentaire