Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- projekt: Slutteprojekt
- Navn : Abdirahman Ahmed
- python version : 3.7.2
- included packages : random,time,matplotlib
- """
- #imports
- import random
- import time
- import matplotlib.pyplot as plt
- #prevent system random from getting struck by time
- random.seed()
- #define our dices
- DICE = [1,2,3,4,5,6]
- #copy the list to prevent namespace complications
- DICE_2 = DICE.copy()
- DICE_3 = DICE.copy()
- #all possible values 1,2 isn't possible with three dices
- values = {
- 3:0,
- 4:0,
- 5:0,
- 6:0,
- 7:0,
- 8:0,
- 9:0,
- 10:0,
- 11:0,
- 12:0,
- 13:0,
- 14:0,
- 15:0,
- 16:0,
- 17:0,
- 18:0,
- }
- #function to get the percentage of a value (easier to do with lambda function)
- def get_percentage():
- #create store list
- LIST = []
- #loop through value keys
- for value in values:
- """
- calculate percentage by value
- exmpl:
- 11* 100/ total(33566) *100/100 <-- to round it to 2 digits
- """
- LIST.append(int((values[value]*100/total)*100)/100)
- #return list
- return LIST
- #function to get a value by rolling two dices
- def roll_three_dices():
- #get 3 random dices values
- numA = random.choice(DICE)
- numB = random.choice(DICE_2)
- numC = random.choice(DICE_3)
- #add the 3 random values
- val = numA + numB + numC
- #return value and the value of every dice
- return val,numA,numB,numC
- #start a timer to get the time the code needs to run (not neccesary)
- start = time.time()
- #total trow counter
- total = 0
- #loops 1000 times - higher accuracy
- for i in range(1000):
- #loops through every value (3,4 etc)
- for value in values:
- #reset trys
- while True:
- #add and total
- total += 1
- #get val
- val,A,B,C = roll_three_dices()
- #if val == value : break loop bc of number is rolled
- if val == value:
- break
- #adds 1 t oval gotten
- values[val]+= 1
- #end timer
- ende = time.time()
- print("Total : ",total)
- #draw data table
- for value in values:
- print("VALUE : ",value," appeared ",values[value]," times //",int((values[value]*100/total)*100)/100,"%")
- print(int((ende-start)*1000)/1000," sec")
- #graph
- #set title
- plt.title("3 DICES")
- #plot data
- plt.plot(list(values.keys()),get_percentage())
- #set axis
- plt.xlabel("Values")
- plt.ylabel("percentage %")
- #render plot
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement