Leo
Leo

Reputation: 89

Lotto draw simulation in Python

import random


n_draws=5000
columns_list= ['1st','2nd','3rd','4th','5th','6th']
data= []
data1=[]

#to create a list of the first picks from 1 through 70
for i in range(n_draws):
    lowballs= (random.sample(range(1,70),6)) 
    data.append(lowballs)
df= pd.DataFrame(data, columns= columns_list)

#to get the powerball #
for i in range(n_draws):
    powerball= (random.sample(range(1,25),1))
    data1.append(powerball)
df1= pd.DataFrame(data1)

#merge both data sets
df2= pd.merge(df, df1, right_index=True, left_index=True)
df2.rename(columns={0:'Powerball'}, inplace=True)

Upvotes: 1

Views: 939

Answers (2)

Paul Whipp
Paul Whipp

Reputation: 16521

OK, writing as I think in a pythonic manner:

I want to generate a lottery pick on demand and then get as many of those in a "dataframe" as I need. Write the basics, push them down and express the problem in python:

import random


def lottery_dataframe(number_required=5000):
    return (lottery_pick() for _ in range(number_required))


def lottery_pick():
    return (*(lottery_digit_pick() for _ in range(6)), powerball_digit_pick())


def powerball_digit_pick():
    return random.randint(1, 25)


def lottery_digit_pick():
    return random.randint(1, 70)

Note that this only generates the data as needed. To view the generated stuff you can wrap it in list e.g.

In [1]: list(lottery_dataframe(4))                                                                                                                                                                                                                                                       
Out[1]: 
[(68, 10, 31, 28, 27, 49, 4),
 (26, 68, 24, 12, 62, 21, 19),
 (16, 20, 23, 52, 70, 36, 22),
 (9, 30, 33, 39, 21, 12, 6)]

Upvotes: 1

Code Different
Code Different

Reputation: 93161

Use numpy's random module:

n_draws = 5000
lowballs = np.random.choice(np.arange(1, 71), size=(n_draws, 6))

df = pd.DataFrame(lowballs, columns=[f'lowball_{i}' for i in range(1,7)])
df['powerball'] = np.random.randint(1, 26, n_draws)

Result:

   lowball_1  lowball_2  lowball_3  lowball_4  lowball_5  lowball_6  powerball
0         41         61         66         63         50          1         17
1         41         19         19         54         65          2         16
2         55         32         16         20         14          2         12
3         69          4         58         53         10         15          8
4         45          2         49          7         46         14         23

Upvotes: 1

Related Questions