Advertisement
Guest User

Untitled

a guest
May 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. """
  2. projekt: Slutteprojekt
  3. Navn : Abdirahman Ahmed
  4.  
  5. python version : 3.7.2
  6. included packages : random,time,matplotlib
  7. """
  8. #imports
  9. import random
  10. import time
  11. import matplotlib.pyplot as plt
  12. #prevent system random from getting struck by time
  13. random.seed()
  14.  
  15. #define our dices
  16. DICE = [1,2,3,4,5,6]
  17. #copy the list to prevent namespace complications
  18. DICE_2 = DICE.copy()
  19. DICE_3 = DICE.copy()
  20.  
  21. #all possible values 1,2 isn't possible with three dices
  22. values = {
  23. 3:0,
  24. 4:0,
  25. 5:0,
  26. 6:0,
  27. 7:0,
  28. 8:0,
  29. 9:0,
  30. 10:0,
  31. 11:0,
  32. 12:0,
  33. 13:0,
  34. 14:0,
  35. 15:0,
  36. 16:0,
  37. 17:0,
  38. 18:0,
  39. }
  40.  
  41. #function to get the percentage of a value (easier to do with lambda function)
  42. def get_percentage():
  43. #create store list
  44. LIST = []
  45. #loop through value keys
  46. for value in values:
  47. """
  48. calculate percentage by value
  49. exmpl:
  50. 11* 100/ total(33566) *100/100 <-- to round it to 2 digits
  51. """
  52. LIST.append(int((values[value]*100/total)*100)/100)
  53. #return list
  54. return LIST
  55.  
  56. #function to get a value by rolling two dices
  57. def roll_three_dices():
  58. #get 3 random dices values
  59. numA = random.choice(DICE)
  60. numB = random.choice(DICE_2)
  61. numC = random.choice(DICE_3)
  62.  
  63. #add the 3 random values
  64. val = numA + numB + numC
  65. #return value and the value of every dice
  66. return val,numA,numB,numC
  67. #start a timer to get the time the code needs to run (not neccesary)
  68. start = time.time()
  69. #total trow counter
  70. total = 0
  71. #loops 1000 times - higher accuracy
  72. for i in range(1000):
  73. #loops through every value (3,4 etc)
  74. for value in values:
  75. #reset trys
  76. while True:
  77. #add and total
  78. total += 1
  79. #get val
  80. val,A,B,C = roll_three_dices()
  81. #if val == value : break loop bc of number is rolled
  82. if val == value:
  83. break
  84. #adds 1 t oval gotten
  85. values[val]+= 1
  86. #end timer
  87. ende = time.time()
  88. print("Total : ",total)
  89. #draw data table
  90. for value in values:
  91. print("VALUE : ",value," appeared ",values[value]," times //",int((values[value]*100/total)*100)/100,"%")
  92. print(int((ende-start)*1000)/1000," sec")
  93.  
  94. #graph
  95. #set title
  96. plt.title("3 DICES")
  97. #plot data
  98. plt.plot(list(values.keys()),get_percentage())
  99. #set axis
  100. plt.xlabel("Values")
  101. plt.ylabel("percentage %")
  102. #render plot
  103. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement