[NEWB]: List with random numbers

E

eltower

Hey all,

I'm trying to write a program in Python for learning purposes which is
meant to:

Generate a random number from 0 to 6
Insert this random number to the end of a list unless the number is
already there
finish with a len(list) = 7

so far, I have this:

import random

random_list = []

while len(random_list) < 8:
j = random.randrange(6)
if (j in random_list):
continue
else:
random_list.append(j)
continue

print random_list


however, I get stuck in an infinite loop.

Any suggestions?

Thank you in advance,

Adri
 
F

faulkner

what you want is impossible. step back a second. you want 7 distinct
ints all between 0 and 5 inclusive. of course you'll loop forever. once
you get all 6 numbers, no matter what you get will already be in your
list.
if you want floats between 0 and 6, say '6 * random.random()'.
random.randrange is equivalent to random.choice(range(*arguments)),
which only deals with whole numbers.
 
S

Simon Forman

eltower said:
Hey all,

I'm trying to write a program in Python for learning purposes which is
meant to:

Generate a random number from 0 to 6
Insert this random number to the end of a list unless the number is
already there
finish with a len(list) = 7

so far, I have this:

import random

random_list = []

while len(random_list) < 8:
j = random.randrange(6)
if (j in random_list):
continue
else:
random_list.append(j)
continue

print random_list


however, I get stuck in an infinite loop.

Any suggestions?

Thank you in advance,

Adri

Maybe this would help:
print random.randrange(6),


3 2 5 4 1 3 1 3 5 0 5 3 4 0 2 2 5 2 2 5 3 1 0 2 0 4 2 4 3 1 3 3 2 3 3 2
1 5 4 2 0 1 5 3 4 1 2 3 5 1 1 5 4 0 3 0 4 4 1 2 1 4 4 5 2 4 5 4 2 5 5 3
5 0 2 3 2 3 5 5 2 0 0 1 5 5 0 0 3 5 3 2 1 4 4 0 1 0 3 1 0 2 0 5 5 2 5 0
5 5 1 0 2 3 1 4 3 3 1 3 5 2 1 4 0 5 3 2 5 0 2 5 3 4 5 5 0 0 3 4 3 1 5 5
3 4 3 4 5 0 3 0 2 5 5 1 3 3 5 3 4 0 3 3 2 5 1 1 1 1 0 3 0 3 5 0 4 5 4 0
1 0 5 2 3 1 1 4 4 3 0 0 0 3 3 5 3 5 4 1 1 0 2 4 5 3 3 1 3 5 5 0 1 4 2 1
0 0 0 3 0 4 5 5 5 5 0 4 1 3 4 0 5 3 0 0 5 1 1 3 4 1 0 5 4 5 0 1 1 5 4 1
2 2 5 2 0 1 1 5 4 5 1 5 5 3 0 3 2 4 3 4 3 2 2 0 3 2 4 2 4 2 3 5 0 4 0 0
1 3 0 4 1 0 0 4 2 4 5 3 5 0 4 2 1 4 4 2 0 0 4 4 1
Traceback (most recent call last):
File "<pyshell#8>", line 2, in -toplevel-
print random.randrange(6),
KeyboardInterrupt


If not, try putting "print random_list" *inside* your while loop.


HTH,
~Simon

P.S. Take a look at the random.shuffle() function... :)
 
M

Marc 'BlackJack' Rintsch

Generate a random number from 0 to 6
Insert this random number to the end of a list unless the number is
already there
finish with a len(list) = 7

so far, I have this:

import random

random_list = []

while len(random_list) < 8:

Well, you said yourself that you finish with a list of length 7. And you
are doing this as long as your list is shorter than 8. 7 < 8 is always
true → infinite loop.
j = random.randrange(6)
if (j in random_list):
continue
else:
random_list.append(j)
continue

print random_list


however, I get stuck in an infinite loop.

Any suggestions?

Do you know `random.shuffle()`?

In [4]: random_list = range(7)

In [5]: random.shuffle(random_list)

In [6]: random_list
Out[6]: [1, 4, 6, 2, 5, 0, 3]

Same effect but more efficient than your approach.

Ciao,
Marc 'BlackJack'
 
E

eltower

Marc said:
Generate a random number from 0 to 6
Insert this random number to the end of a list unless the number is
already there
finish with a len(list) = 7

so far, I have this:

import random

random_list = []

while len(random_list) < 8:

Well, you said yourself that you finish with a list of length 7. And you
are doing this as long as your list is shorter than 8. 7 < 8 is always
true → infinite loop.
j = random.randrange(6)
if (j in random_list):
continue
else:
random_list.append(j)
continue

print random_list


however, I get stuck in an infinite loop.

Any suggestions?

Do you know `random.shuffle()`?

In [4]: random_list = range(7)

In [5]: random.shuffle(random_list)

In [6]: random_list
Out[6]: [1, 4, 6, 2, 5, 0, 3]

Same effect but more efficient than your approach.

Ciao,
Marc 'BlackJack'

Holey moley.

random.shuffle() seems to be just the answer that I needed, thank you
very much :)

Adri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top