473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[NEWB]: List with random numbers

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

Aug 20 '06 #1
4 1882
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.

eltower wrote:
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
Aug 20 '06 #2

eltower wrote:
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:
>>while True:
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... :-)

Aug 20 '06 #3
In <11*********************@b28g2000cwb.googlegroups. com>, eltower wrote:
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'
Aug 20 '06 #4
Marc 'BlackJack' Rintsch wrote:
In <11*********************@b28g2000cwb.googlegroups. com>, eltower wrote:
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

Aug 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team...
5
by: Alexandre | last post by:
Hi, Im a newb to dev and python... my first sefl assigned mission was to read a pickled file containing a list with DB like data and convert this to MySQL... So i wrote my first module which...
4
by: Bart Nessux | last post by:
New to Python... trying to figure out how to count the objects in a list and then map the count to the objects or convert the list to a dict... I think the latter would be better as I need a number...
4
by: Jesse Noller | last post by:
Hello - I'm probably missing something here, but I have a problem where I am populating a list of lists like this: list1 = list2 = list3 = main_list =
20
by: William Stacey [MVP] | last post by:
int list = {1,2,3,4,5,6}; Function to randomize the list? Cheers! -- William Stacey, MVP
2
by: eastband | last post by:
This is for homework... I need to generate a list of random numbers on command ========================================================== ? random 5 Result: ...
2
by: philip | last post by:
hello, i am confused with the following code, i suppose i will get 5 numbers in the following code, but i always get 3 numbers, would u please tell me why? thanks. List<inta = new List<int>();...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
16
by: skip | last post by:
The thread on sorting in Python 3 got me to thinking. How could I sort a list of complex numbers using key? As expected: Traceback (most recent call last): File "<stdin>", line 1, in...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.