function - Generating random number with different digits -


so need write program generates random numbers 100 999, , tricky part digits in number can't same.

for example: 222, 212 , on not allowed.

so far, have this:

import random  = 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 , != b , b != c):     print a,b,c 

as can see, generate 3 digits separately. think it's easier check if there same digits in number.

so, want loop generate numbers until requirements met (now either prints blank page or number). note generates once , have open program again.

in c++ did 'while loop'. , here don't know how it.

and 1 more question. how can code number(quantity) of random numbers want generate? more specific: want generate 4 random numbers. how should code it?

p.s. thank answers , suggestions, keen on learning new techniques , codes.

to loop until it's ok can use while in python:

from random import randint  = randint(1, 9) b = randint(0, 9) c = randint(0, 9) while not (a!=b , b!=c , c!=a):     = randint(1, 9)     b = randint(0, 9)     c = randint(0, 9) 

you can put in function:

def generate_number():     = randint(1, 9)     b = randint(0, 9)     c = randint(0, 9)     while not (a!=b , b!=c , c!=a):         = randint(1, 9)         b = randint(0, 9)         c = randint(0, 9)     return (a, b, c) 

and if want n such numbers (they not numbers since (a, b, c) tuple of 3 int values), can call n times:

for in range(n):     print(generate_number()) 

if prefer formatting values, can do:

for in range(n):     print('%d %d %d'%generate_number()) # old style formatting     print('{} {} {}'.format(*generate_number())) # new style 

finally, can use n command line:

import sys n = sys.argv[1] 

or can ask directly:

n = int(input("please enter number: ")) # in python2.x you'd use raw_input instead of input 

you'll exception if value cannot converted; can catch exception , loop generation of numbers.

putting typical main construct:

from random import randint import sys  def generate_number():     = randint(1, 9)     b = randint(0, 9)     c = randint(0, 9)     while not (a!=b , b!=c , c!=a):         = randint(1, 9)         b = randint(0, 9)         c = randint(0, 9)     return (a, b, c)  def main():     n = sys.argv[1]     in range(n):         print('{} {} {}'.format(*generate_number()))  if __name__=='__main__':     main() 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -