-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment-2.py
More file actions
79 lines (61 loc) · 2.23 KB
/
Assignment-2.py
File metadata and controls
79 lines (61 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 27 15:48:03 2022
@author: draco
"""
import numpy as np
import matplotlib.pyplot as plt
import random
from scipy.stats import norm
##call dicethrow with 1st variable input as the number of throws, second as no. of dice and the mode.The default mode is sum. In case of abs diff the number mode =! sum and to be noted that it only works when the number of dice is two \n" )
## question 1,2,5 solution ##
def dicethrow(no_throws,no_dice,mode ='add'):
dice_sum_outcome = []# the elements in number of sum should be the sum of the dices.
#def throws ()
for i in range (1,no_throws+1):
#print("number of throw", i)
dice_face = []
for j in range(1,no_dice+1):
dice = random.randint(1,6)
#print(dice)
dice_face.append(dice)
#print (dice_face)
if mode == 'add':
out_add = sum(dice_face)
#print (out_add)
else:
######## only in case of two dice ######## for absolute difference##
print(dice_face)
out_add = abs(dice_face[0]-dice_face[1])
dice_sum_outcome.append(out_add)
return dice_sum_outcome
####call diceinteg for Q 6. plot ####
def diceinteg(no_throws, no_dice):
Dice=np.zeros((6 , no_dice))
for r in range (no_dice) :
Dice [:,r]=np.array ( [ 0 , 1 , 2 , 3 , 4 , 5 ] )*6**r
#print ( Dice )
output_sum = np.zeros(no_throws)
for i in range (no_throws):
#print("throw number",i)
for j in range(no_dice):
dice_output = random.choice(Dice[:,j])
output_sum[i] += dice_output
#print("the dice shows", dice_output)
dice_sum_outcome = output_sum
return (dice_sum_outcome)
no_dice=2
no_throws=10000000
#dice_sum_output = dicethrow(no_throws,no_dice)
dice_sum_output = diceinteg(no_throws,no_dice)
mu = np.mean(dice_sum_output)
sigma = np.var(dice_sum_output)
print(mu,sigma)
########## Plot ########
plt.hist(dice_sum_output)
plt.ylabel("PDF")
plt . xlabel("SUM")
plt.tight_layout()
plt.title(" %s dice with %s throws has a mean %s and variance %s." %(no_dice,no_throws,mu,sigma))
plt.show()