dimanche 15 mars 2020

Lotto draw simulation in Python

  • Problem: I'm trying to create a dataframe that has n number of simulations for lotto drawings, * Solution: Generate random int from 1 through 70 for the lowball numbers and append each iteration to a dataframe. Similar process for the powerball from 1 to 25. Finally, merged both dataframes
  • Output: A dataframe with the 5k simulations
  • Help: Is there a more pythonic way to achieve the same?
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)




Aucun commentaire:

Enregistrer un commentaire