[Tutor] Random number generator
Kent Johnson
kent37 at tds.net
Fri Oct 28 13:48:11 CEST 2005
Johan Geldenhuys wrote:
> After I tested the previous code, I noticed that the odds is 1:49 that a
> duplicate number can be found in the 6 digit range (and it happended)
> and that 0 can also be found.
Look at random.sample() for a simpler way to do this.
Kent
>
> Here is the fix:
>
> import random
>
> def randnum():
> c = []
> for x in range(6):
> s = random.randrange(0, 50)
> if s not in c:
> c.append(s)
> else:
> return
> print ('Randoms: ',c)
>
> c = []
>
> if __name__ == '__main__':
>
> for x in range(10): # For number of 6-digit sequences
> randnum()
>
> AYS?
>
>>
>>
>> R. Alan Monroe wrote:
>>
>>>>Hey all,
>>>>I am trying to create a program that draws 6 numbers between 1 and 49 at random for creating lottery tickets. I want to have a better chance when I play. Can anyone help me code this or show me how
>>>>to, please?
>>>>
>>>>
>>>
>>>Create (empty for now) list to hold your final numbers.
>>>Have a for-loop that runs 6 times using range(6)
>>>Inside the loop, just pick a random number and append it to your final
>>>number list.
>>>
>>>Alan
>>>
>>>_______________________________________________
>>>Tutor maillist - Tutor at python.org
>>>http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>>>
>>------------------------------------------------------------------------
>>
>>_______________________________________________
>>Tutor maillist - Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
--
http://www.kentsjohnson.com
More information about the Tutor
mailing list