## Please help with creating a unique random 3 digit number

This is the place for queries that don't fit in any of the other categories.

### Please help with creating a unique random 3 digit number

I'm doing the following code which is part of a larger program, in general, what the code is to generate a 3-digit number randomic, fulfilling this condition should not be repeated ie: 366, 225, 525 etc. .. The code validates but in some cases when the number is repeated the program crashes .. The idea is to generate the number if it is repeated again until you create it and throw the value ...

Code: Select all
`import randomdef separar(num):    b = num % 10    e = num / 10    c = e % 10    d = e / 10    lista = [d, c, b]    return listadef compara(l1, l2):    c1 = len(l1)    cont = 0    cont1 = 0    for i in range(c1):        for j in range(c1):            if(i == j):                if(l1[i] == l2[j]):                    cont = cont + 1                elif(l1[i] == l2[j]):                    cont1 = cont1 + 1                print 'Posee', cont, 'fijas'                print 'Posee', cont1, 'picas \n'def repetido(lis1):    flat = False    c1 = len(lis1)    for i in range(c1):        for j in range(i + 1, c1):            if(lis1[i] == lis1[j]):                flat = True    return flatdef numcompu():    a = random.choice(range(100, 999))    lis1 = separar(a)    fla = repetido(lis1)    if (fla):        numcompu()        print 'grave'    else:        return aran = numcompu()print ran`
Last edited by Yoriz on Wed Jun 12, 2013 10:54 pm, edited 2 times in total.
Reason: Added code tags and re-indented the code, edited title

Posts: 1
Joined: Wed Jun 12, 2013 9:47 pm

### Re: random number

Please check the indentation i added back is the same as your code and also add a full traceback of the error you are getting and please read the following thread.
http://www.python-forum.org/viewtopic.php?f=6&t=145

Your code looks overly complicated, how about the following code.
1. Create the range of numbers
2. Shuffle the range of numbers
3. Pop a number off the range of numbers
Code: Select all
`import randomnumbers = [number for number in xrange(100, 999)]random.shuffle(numbers)print numbers.pop()`
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1444
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: random number

Yoriz wrote:numbers = [number for number in xrange(100, 999)]

This will probably blow your mind but this is a good time to use range() :)
Code: Select all
`numbers = range(100, 999)`

Or maybe even
Code: Select all
`numbers = list(xrange(100, 999))`

if you just really like xrange().
Join the #python-forum IRC channel on irc.freenode.net for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from

micseydel

Posts: 2032
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Please help with creating a unique random 3 digit number

Thanks, minds not blown i just didnt see the tree for the woods.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1444
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Please help with creating a unique random 3 digit number

and to be 100% correct it's
Code: Select all
`range(100, 1000)`
:-).
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm