Re: [Tutor] 6 random numbers

2011-10-17 Thread Robert Sjoblom
>     print random.randrange(1,42)
> If you want to collect the intermediate results, either store them in a
> list:
>
> results = []
> for i in range(6):
>     results.append(random.randrange(1,42))

Gmail has removed the "Reply All" function for some ABSOLUTELY
RIDICULOUS REASON, so I think people missed my reply on this.

randrange is a horrible way to generate lottery numbers because it
doesn't generate unique numbers, and as for lotteries I don't think
I've seen a single one where they put the balls back before selecting
a new. random.sample is better because it does (for this purpose, at
least) and it stores the result in a list automatically.

Let's hope this email goes to the right thread. :/


-- 
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 6 random numbers

2011-10-16 Thread Stefan Behnel

ADRIAN KELLY, 16.10.2011 21:43:

anyone know how i would go about printing 6 random numbers


print(75, 45, 6, 35, 36472, 632)

Numbers were generated by typing randomly on my keyboard.

Sorry-for-taking-it-all-too-literally-ly,

Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 6 random numbers

2011-10-16 Thread Steven D'Aprano

ADRIAN KELLY wrote:

hello all,
anyone know how i would go about printing 6 random numbers, i know i could copy and paste 6 times (which would work) but i was thinking about a while loop, ie. while lottery_numbers.count is <7. 
Is it possible to code this? is it possible to count random variables? i am trying to keep the program as simple as possible, cheers


any help would be welcome, 


import random
lottery_numbers=random.randrange(1,42)
print lottery_numbers


Whenever you want to do something repeatedly, a for-loop or while-loop 
is usually the answer. If you know how many times you need to do it, use 
a for-loop:


for i in range(6):
print random.randrange(1,42)


If you don't know how many times, use a while-loop:

total = 0
while total < 100:
total += random.randrange(1,42)
print total


If you want to collect the intermediate results, either store them in a 
list:


results = []
for i in range(6):
results.append(random.randrange(1,42))

or use a list comprehension, which is syntactic sugar for a for-loop:

results = [random.randrange(1,42) for i in range(6)]



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 6 random numbers

2011-10-16 Thread Robert Sjoblom
>> hello all,
>> anyone know how i would go about printing 6 random numbers, i know i could 
>> copy and paste 6 times (which would work) but i was thinking about a while 
>> loop, ie. while lottery_numbers.count is <7.
>> Is it possible to code this? is it possible to count random variables? i am 
>> trying to keep the program as simple as possible, cheers
>>
>> any help would be welcome,
>>
>> import random
>> lottery_numbers=random.randrange(1,42)
>> print lottery_numbers
>>
>
> Following example may be useful:
> x = [random.randrange(1, 1+random.randrange(42) for _ in range(100)]
>
> Useful read:
> http://docs.python.org/tutorial/datastructures.html

This wouldn't work because if you're making a lottery-type program,
you can't generate the same number again. A solution would be to check
if, say we stored the numbers in a list, len(set(numbers)) == 7 and if
it's not append another (random) number. This would be done until
len(set(numbers)) == 7. However, it's a bit tedious and the random
module offers a much better option:

import random
numbers = random.sample(range(1,42), 7)

random.sample returns k unique random elements from a population
sequence; in this case the population sequence is range(1, 42) (I
think python 2.x it'd be xrange()?) and the second argument would be
the number of elements we want.

>>>random.sample(range(1,42), 7)
[16, 29, 17, 2, 12, 36, 10]#this list is in selection order!

-- 
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 6 random numbers

2011-10-16 Thread शंतनू
On 17-Oct-2011, at 1:13 AM, ADRIAN KELLY wrote:

> hello all,
> anyone know how i would go about printing 6 random numbers, i know i could 
> copy and paste 6 times (which would work) but i was thinking about a while 
> loop, ie. while lottery_numbers.count is <7. 
> Is it possible to code this? is it possible to count random variables? i am 
> trying to keep the program as simple as possible, cheers
> 
> any help would be welcome, 
> 
> import random
> lottery_numbers=random.randrange(1,42)
> print lottery_numbers
>   

Following example may be useful:
x = [random.randrange(1, 1+random.randrange(42) for _ in range(100)]

Useful read: 
http://docs.python.org/tutorial/datastructures.html

-- 
shantanoo
http://xkcd.com/221/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor