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

Postby dador33 » Wed Jun 12, 2013 9:50 pm

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 random


def separar(num):
    b = num % 10
    e = num / 10
    c = e % 10
    d = e / 10
    lista = [d, c, b]
    return lista


def 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 flat


def numcompu():
    a = random.choice(range(100, 999))
    lis1 = separar(a)
    fla = repetido(lis1)
    if (fla):
        numcompu()
        print 'grave'
    else:
        return a

ran = 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
dador33
 
Posts: 1
Joined: Wed Jun 12, 2013 9:47 pm

Re: random number

Postby Yoriz » Wed Jun 12, 2013 10:17 pm

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 random

numbers = [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!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: random number

Postby micseydel » Thu Jun 13, 2013 12:04 am

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!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1222
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

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

Postby Yoriz » Thu Jun 13, 2013 12:22 am

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!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

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

Postby ochichinyezaboombwa » Thu Jun 13, 2013 6:05 am

and to be 100% correct it's
Code: Select all
range(100, 1000)
:-).
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests