# import the library numpy as np
# import the library matplotlib.pyplt as plt
import numpy as np
import matplotlib.pyplot as plt
# set a seed for your calculations so that they are reproducible
np.random.seed(12345)
# generate 50 random integers from 1 to 50 using random.randint() and make a
# frequency histogram
array1= np.random.randint (low = 1, high= 50, size= 50)
print (array1)
[35 38 30 2 37 42 38 35 30 2 15 42 28 17 10 12 14 11 18 19 39 8 44 40
24 30 32 34 28 24 37 1 48 4 6 16 12 15 18 44 6 35 9 6 35 45 6 20
13 15]
plt.hist (array1, 5)
# generate 10000 random integers from 1 to 50 using random.randint() and make a
# frequency histogram
# compare with previous histogram
array2= np.random.randint (low = 1, high= 50, size= 1000)
print (array2)
[17 28 39 25 33 44 6 11 13 23 13 42 19 19 47 24 22 8 18 1 27 45 15 27
44 31 36 33 4 1 7 3 45 38 9 44 23 22 2 33 6 41 21 8 6 46 19 9
12 19 6 38 10 45 39 2 47 26 44 1 14 9 9 19 48 16 27 33 45 33 23 6
20 16 35 29 8 5 40 4 23 33 14 21 2 26 45 6 3 20 25 49 42 36 6 25
46 42 15 20 43 47 25 31 13 12 37 45 26 41 26 33 34 29 7 33 14 38 24 41
13 19 30 2 26 1 27 13 27 24 32 14 6 49 1 11 32 3 43 20 16 35 44 18
13 15 46 1 28 40 4 18 45 26 47 45 11 35 41 17 13 15 23 10 29 33 17 46
8 26 14 38 44 35 41 6 49 37 7 9 39 44 34 34 31 1 14 41 43 49 33 22
14 18 22 14 35 19 16 14 25 16 37 15 45 19 48 24 45 35 26 4 18 9 46 46
15 9 33 17 30 7 8 44 35 24 18 28 48 45 8 43 12 39 4 30 3 47 46 27
46 31 20 38 26 15 17 17 4 33 33 17 39 18 5 15 34 49 22 44 34 39 6 20
46 44 40 6 25 35 48 40 20 47 38 43 4 1 24 28 14 29 45 33 1 11 33 35
8 1 10 42 19 27 28 10 35 13 19 17 10 4 5 10 23 16 41 32 10 28 45 8
19 32 26 33 39 9 8 39 9 38 15 21 13 14 36 32 24 21 39 47 43 42 33 18
20 12 43 16 45 19 42 33 38 42 7 30 5 33 21 2 22 9 10 25 16 35 40 20
39 14 23 29 44 30 45 3 13 18 47 41 32 25 42 22 13 39 46 25 20 48 44 38
39 31 41 14 6 3 31 42 42 48 47 14 30 39 48 15 32 45 22 17 49 43 14 45
35 32 15 42 40 44 49 32 12 23 24 3 12 48 37 44 34 20 37 4 29 34 19 20
39 14 36 25 26 48 11 29 35 33 43 41 21 11 38 10 35 28 20 43 14 14 49 16
40 5 22 46 29 36 7 22 18 10 40 33 8 45 2 48 21 21 10 19 25 49 44 23
3 1 17 32 33 3 9 28 21 14 6 46 43 29 42 23 14 38 48 12 16 20 34 11
25 26 26 20 2 39 25 37 1 40 16 16 25 2 49 49 13 4 6 23 36 11 40 26
3 30 41 47 16 1 31 4 38 31 14 3 36 3 48 5 26 38 27 6 44 47 48 38
20 49 42 15 44 36 37 45 45 22 14 49 4 47 44 13 22 21 23 22 7 12 23 46
48 15 49 38 45 34 22 2 23 21 23 27 26 14 32 30 38 19 17 44 40 15 20 41
31 1 34 12 14 43 47 18 33 10 8 14 49 40 35 24 47 42 34 16 15 42 49 39
32 37 2 28 21 9 12 49 20 2 37 14 30 24 26 33 24 9 43 35 2 43 8 1
47 27 13 37 13 23 18 29 11 28 16 38 17 32 1 45 35 28 44 2 37 2 7 49
49 12 46 40 19 32 32 41 25 31 15 38 37 32 49 36 42 7 1 36 11 12 38 22
7 40 40 31 42 13 28 3 25 32 9 26 30 4 37 14 22 24 45 28 2 24 19 27
34 12 42 5 17 15 15 26 37 32 22 1 11 19 2 32 6 36 4 3 12 44 1 49
27 6 20 38 46 45 29 27 30 8 27 8 3 24 15 18 3 47 32 16 13 23 31 1
3 25 36 11 10 4 49 36 44 48 41 2 32 36 21 27 46 46 16 27 39 46 4 42
8 26 6 33 7 11 27 8 6 24 39 21 36 11 31 5 3 2 13 4 46 48 17 49
35 26 2 42 2 36 19 4 27 6 44 22 26 4 32 47 25 29 41 5 30 16 16 27
1 10 27 37 6 7 48 12 6 4 17 17 44 22 34 23 19 38 45 33 13 36 32 4
33 31 33 41 23 13 8 48 25 44 17 44 16 28 13 7 33 11 36 27 27 49 48 29
7 44 20 3 21 6 20 31 39 6 43 2 5 34 27 15 33 4 33 33 14 33 40 25
12 43 22 14 16 3 37 33 40 36 9 40 16 15 43 3 15 33 3 20 47 5 28 25
22 34 18 16 9 21 29 31 35 8 47 37 24 25 14 44 41 41 23 24 16 31 27 1
19 41 43 26 17 17 22 49 19 7 13 3 18 17 5 37 27 4 17 19 5 39 26 30
42 1 8 42 39 26 20 11 20 4 35 25 3 45 25 49]
plt.hist (array2, 5)
# generate 1000 floating point numbers uniformly distributed from 1 to 100 and make a
# frequency histogram
z= np.random.uniform(low= 1, high=100, size = 1000)
plt.hist (z, 10)
# generate 1000 floating point numbers normally distributed about a mean of 50
# with a standard deviation of 5 and make a frequency histogram
#
x = np.random.normal (size = 1000)
y = 50 + x * (5)
plt.hist (y, 10)
# generate 1000 floating point numbers normally distributed about a mean of 50
# with a standard deviation of 5 and make a density histogram; compare with frequency
# histogram
plt.hist (y, 10, density= True)
# import random and set seed
import random
random.seed (123456)
# Simulate drawing a single ball; each ball has a number from from 1 to 50
random.randint (1,50)
# Simulate drawing 100 balls and keep track of the number of balls
# that have a number less than 25
x = random.randint (1, 50)
n = 100
less_list= []
for i in range (0, n) :
x = random.randint (1, 50)
if x < 25:
less_list.append (random.randint (1, 50))
print (less_list)
print (f'out of {n} simulations {len(less_list)} random numbers were less than 25 ')
[1, 4, 2, 15, 2, 25, 3, 50, 29, 34, 7, 7, 6, 22, 29, 24, 16, 18, 6, 14, 2, 28, 11, 1, 39, 33, 48, 50, 41, 28, 4, 47, 14, 35, 24, 13, 33, 36, 37, 27, 12, 10]
out of 100 simulations 42 random numbers were less than 25
# Calculate discrete probability that you will draw a ball with a number <25 using 10,000
# simulations
x = random.randint (1, 50)
n = 10000
less_list= []
for i in range (0, n) :
x = random.randint (1, 50)
if x < 25:
less_list.append (random.randint (1, 50))
d= (len(less_list))
print (d)
print (d/n)
print (f'the discrete probability of drawing a ball with a number less than 25 out of {n} simulations is {d/n}')
4844
0.4844
the discrete probability of drawing a ball with a number less than 25 out of 10000 simulations is 0.4844
# Now suppose you are playing a game where you draw a ball. You win if you get a number
# <25 and lose otherwise. Write a function which draws a single ball and returns True
# if the number is <25 and false if it is >= 25
# Test out your function
def draw (n_times) :
outcomes = ['true', 'false']
n_true = 0; n_false = 0
for i in range (0, n_times) :
toss= random.randint (1,50)
if (toss < 25) :
n_true = n_true +1
print ('true')
else :
n_false = n_false + 1
print ('false')
return ( [n_true, n_false])
draw (1)
true