In python 3, I have a simple dice roller program. What it does is ask the user for the amount of sides of a dice and how many times they would like to roll it.
This is accomplished by creating a list and each sublist represents a dice side. Every time a random number is generated, it is appended to the according sublist.
The results are displayed with a simple print procedure.
My query is how can I use multiprocessing to make it faster as it takes ~21 minutes to do 1 million rolls.
The code for the program is as follows:
import time
import random
roll = []#List for the results
def rng(side,reps):#rolls the dice
for i in range(reps):
land = random.randint(1,side)
print(land)
roll[land-1].append(land)
def printR(side,reps):#Prints data
for i, item in enumerate(roll):
print('D'+str(i+1),'=''total ',total)
def Main():
side = int(input('How many sides is the dice'))
reps = int(input('How many rolls do you want to do?'))
for i in range(side):#Creates empty arrays corresponding to amount of sides
roll.append([])
t0= time.clock()#Start timing dice roller
rng(side,reps)
t1 = time.clock()#End timing of dice roller
printR(side,reps)#Print data
times = t1 - t0#Time
print(round(times,3),'seconds')
Main()