Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python] Powerball number generator
03-20-2010, 09:39 PM (This post was last modified: 03-22-2010 01:44 AM by Alphablend.)
Post: #1
[Python] Powerball number generator
http://openbookproject.net/pybiblio/prac...erball.php

Powerball number generator

Description
To win the Powerball lottery (an extremely unlikely event so don't waste your time) you have to pick six numbers correctly. The first five numbers are drawn from a drum containing 53 balls and the sixth is drawn from a drum containing 42 balls. The chances of doing this are 1 in 120,526,770. Write a program to generate a set of Powerball numbers by utilizing the choice function in Python's random module.

Input
Ask the user how many sets of Powerball numbers he or she would like.

Output
The program will print each set of Powerball numbers in numeric order.

Sample session
Official (but fruitless) Powerball number generator

How many sets of numbers? 3

Your numbers: 3 12 14 26 47 Powerball: 2
Your numbers: 1 4 31 34 51 Powerball: 17
Your numbers: 10 12 49 50 53 Powerball: 35

Code:
from random import randint
def BubbleSort(l):
    e=0
    for i in range(len(l)-1):
        for j in range(len(l)-1):
            if l[j]>l[j+1]:
                e=l[j+1]
                l[j+1]=l[j]
                l[j]=e
    return l
def MyPrint(L,pb):
        BubbleSort(L)
        print "Your numbers: %d %d %d %d %d Powerball: %d"%(L[0],L[1],L[2],L[3],L[4],pb)

print "Official (but fruitless) Powerball number generator"
No=int(raw_input("How many sets of numbers? "))
for i in range(No):
    numbers=[]
    powerball=randint(1,42)
    i=0
    while (i!=5):
        numbers.append(randint(1,53))
        i+=1
    for i in range(len(numbers)-1):
        for j in range(len(numbers)-1):
            if i==j:
                j+=1
            if numbers[i]==numbers[j] or powerball==numbers[j]:
                numbers[j]=randint(1,53)
                j-=1
    MyPrint(numbers,powerball)

Few notes:
After you choose how many sets of numbers you want, the first 'while' loop gives some random numbers to our powerball list. After that, the 2 'for' loops check if we have double numbers in our list. Because after we draw one ball, that one cannot be chosen again and it is out of the game.

Then the process repeats for the rest of the sets.

If you have any questions or suggestions how to improve the code write it down.Big Grin

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
03-20-2010, 10:03 PM
Post: #2
RE: [Python] Powerball number generator
Very good task i can say. Another probability task !
And good use of bubble sort.
One suggestion, length of that array is always (1..5),
so in that bubblesort() function you don't need to use len()
function, you can just put 5 already.
Everything is just okay !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
03-20-2010, 10:59 PM
Post: #3
RE: [Python] Powerball number generator
Well, I made this BubbleSort function before, and I've just copied it. However, later in the program I could have just used number 5. But my habit now is to always call the function len(list) because don't always know the length of list.

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
03-21-2010, 01:18 PM (This post was last modified: 03-21-2010 01:18 PM by codecaine.)
Post: #4
RE: [Python] Powerball number generator
You could also use no loops and randomize a list Smile. For example,

PHP Code:
import random;

def lotto():
    
mylist range(54); #list from 0 to 53
    
random.shuffle(mylist); #shuffle the balls aka list
    
return mylist#return lotto numbers random

#you can just keep calling this function for shuffling of numbers and use slices for now many numbers you want to show in a list :)
todayNumbers lotto(); #return todays numbers
print('Todays lucky numbers are: ' str(todayNumbers[0:5])) #show first 5 lotto 
Visit this user's website Find all posts by this user
Quote this message in a reply
03-21-2010, 07:59 PM
Post: #5
RE: [Python] Powerball number generator
@codecaine
wow thanks. i will have to study the random a bit more. 'shufftle' is very helpful here. Smile
i'm still thinking like java programmer. Big Grin

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
03-21-2010, 09:05 PM
Post: #6
RE: [Python] Powerball number generator
Yeah, mixing languages in the beginning is hard.
One experienced programmer said : "Don't learn new language until you finish the one that your started".
Also you need to adjust your brain to think on a certain language !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
03-22-2010, 11:00 AM (This post was last modified: 03-22-2010 11:03 AM by codecaine.)
Post: #7
RE: [Python] Powerball number generator
Anytime my friend Smile There is a book I am readying over here. Its python "gray hat python programming". When I get some time I will make some examples. I should you how to use ctypes module in python and load dll files or if you on unix based systems .so files. Oh yea there is also a sort function built in python. mylist.sort() for it to change to list or if you wanted it to return a sorted list without changing the list itself do sorted(mylist)
Visit this user's website Find all posts by this user
Quote this message in a reply
03-22-2010, 07:57 PM
Post: #8
RE: [Python] Powerball number generator
@codecaine
I was planning to take some time and study some sort algorithms. That is why I didn't use Python's built in sort. And if some beginner runs across this post, he could see an example of bubble sort. (Not that I'm a pro or something.... Big Grin).

My teecha said that in this semester of my 'Introductions to computers' we will continue Python and that we will learn more about Databases, Networks and Object orientated programming (GUI) in Python.
I was so excited and I stared jumping like a kid who got a candy... Big Grin

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
03-23-2010, 04:21 AM
Post: #9
RE: [Python] Powerball number generator
(03-22-2010 11:00 AM)codecaine Wrote:  Anytime my friend Smile There is a book I am readying over here. Its python "gray hat python programming". When I get some time I will make some examples. I should you how to use ctypes module in python and load dll files or if you on unix based systems .so files. Oh yea there is also a sort function built in python. mylist.sort() for it to change to list or if you wanted it to return a sorted list without changing the list itself do sorted(mylist)
That built in function for sorting, is it quick sort ???

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
03-25-2010, 05:49 AM
Post: #10
RE: [Python] Powerball number generator
They have different type of sorting algorithms heres a link to show you http://wiki.python.org/moin/HowTo/Sorting
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: