Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

[NEWB]: List with random numbers

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


acachinero at gmail

Aug 19, 2006, 11:14 PM

Post #1 of 5 (224 views)
Permalink
[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

--
http://mail.python.org/mailman/listinfo/python-list


faulkner612 at comcast

Aug 19, 2006, 11:23 PM

Post #2 of 5 (205 views)
Permalink
Re: [NEWB]: List with random numbers [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list


rogue_pedro at yahoo

Aug 19, 2006, 11:28 PM

Post #3 of 5 (204 views)
Permalink
Re: [NEWB]: List with random numbers [In reply to]

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... :-)

--
http://mail.python.org/mailman/listinfo/python-list


bj_666 at gmx

Aug 19, 2006, 11:32 PM

Post #4 of 5 (205 views)
Permalink
Re: [NEWB]: List with random numbers [In reply to]

In <1156054456.520655.49040[at]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'
--
http://mail.python.org/mailman/listinfo/python-list


acachinero at gmail

Aug 19, 2006, 11:40 PM

Post #5 of 5 (205 views)
Permalink
Re: [NEWB]: List with random numbers [In reply to]

Marc 'BlackJack' Rintsch wrote:
> In <1156054456.520655.49040[at]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

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.