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

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

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

### Re: random number

http://www.python-forum.org/viewtopic.php?f=6&t=145

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()
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
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().
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

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

Thanks, minds not blown i just didnt see the tree for the woods.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

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

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

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