# 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
rand_seed = np.random.seed(123456)
# generate 50 random integers from 1 to 50 using random.randint() and make a
# frequency histogram
rand_seed = np.random.randint (low = 1, high = 50, size = 50)
print(rand_seed)
[ 2 43 44 48 44 28 33 24 37 9 11 11 13 12 21 44 48 23 39 35 15 7 43 3
48 5 31 46 24 21 21 46 6 24 37 12 36 30 40 17 35 19 16 48 33 28 21 18
11 47]
plt.hist(rand_seed, bins = 10)
plt.show()
# generate 10000 random integers from 1 to 50 using random.randint() and make a
# frequency histogram
# compare with previous histogram
rand_seed_2 = np.random.randint(low = 1, high = 50, size = 10000)
plt.hist(rand_seed_2, bins = 10)
plt.show()
# generate 1000 floating point numbers uniformly distributed from 1 to 100 and make a
# frequency histogram
rand_seed_3 = np.random.uniform(low = 1, high = 50, size = 10000)
plt.hist(rand_seed_2, bins = 10)
plt.show()
# generate 1000 floating point numbers normally distributed about a mean of 50
# with a standard deviation of 5 and make a frequency histogram
y = np.random.normal(50,5, size = 1000)
print (y)
plt.hist(y, bins = 10)
plt.show()
[55.07294643 52.54980807 45.98435695 51.28147505 44.74959045 47.18715212
51.83277877 54.61174905 45.91807307 48.9467544 49.26543427 49.70591772
44.54853647 45.91097666 52.51550361 46.34660941 45.85242557 55.32741253
51.15482762 54.36003798 50.79678757 43.41530067 57.53201418 46.02675466
53.54048252 45.4200289 47.28080756 50.06962389 50.705185 56.99050755
47.07828865 42.69336974 55.95862092 45.70709039 49.20861069 46.48954837
50.00162052 37.14519304 46.62713951 50.50901597 56.66437087 49.3910032
50.03216526 53.9095753 52.55818828 50.04046454 52.19341187 55.12637981
43.43119789 47.13818119 54.78656001 45.79177005 50.4123032 49.09337971
57.64477186 49.53945843 52.12113843 50.38792241 45.3427766 57.79481636
53.23105152 50.71962809 50.5181733 44.28846187 47.87030288 54.23769696
46.84820137 44.08694056 49.23956816 44.3919073 42.24078063 51.51830309
48.61114207 51.62483257 58.26199835 46.00021993 58.30504007 53.75964635
51.91774737 49.6004785 47.82436838 53.33996306 52.82136031 57.61876319
48.36527816 49.29473954 53.43500858 54.79302883 52.15745935 51.30904638
46.75994238 53.8105294 45.37570414 46.16220272 46.20307736 49.45255856
41.86696196 58.45795581 49.99648456 53.09275155 43.32687357 52.66935358
50.5186224 42.39270131 53.41792577 48.01489692 44.47482955 55.42123062
57.0114587 54.58968093 43.17066056 48.11377849 49.95969452 52.40321035
58.62743 47.4407764 43.92717483 39.27828215 60.71764803 50.69474642
47.27236802 52.10676978 49.91698357 51.31759251 50.74243102 55.28823066
52.54529814 49.81197499 52.88621946 50.85497594 52.63843204 43.21673834
40.10932355 49.58145483 42.19111162 54.08626242 58.76108096 50.69359531
56.28683964 56.30272689 53.24325959 59.62524847 43.65871123 54.71732144
46.3533314 50.42422437 44.02548326 52.06123286 56.09121101 46.39069129
51.59934571 45.50794888 49.83982578 38.81065789 49.0308639 44.47374797
54.03076897 45.53178968 47.22258831 53.85350171 48.17411982 53.99295381
46.97154326 53.10507641 52.05793378 50.00232446 54.29297663 50.19843807
51.52366563 49.44217123 54.34783462 51.62373714 50.95873371 43.91627182
48.95618501 49.47593924 51.23495326 47.80463519 46.83659694 54.19296457
55.4065364 48.36424985 53.78814097 45.33705104 45.7907883 51.61479592
47.49066932 46.73890257 53.05016561 52.04869532 58.17774144 47.50896828
49.46824904 45.78269758 59.27438373 47.17608715 53.12373033 49.07220055
48.50810696 48.96320327 57.66550234 52.14724127 44.2849898 47.70128221
54.46498695 52.13896458 56.86732958 44.94280007 54.32868841 45.72091216
56.14957874 48.81315826 55.83203665 50.64434444 49.8931175 52.48468442
53.67796484 51.98145632 55.74514649 59.04086162 49.5625329 43.92548352
55.69323778 48.75670135 49.80395766 53.15353904 57.23057353 52.04868412
38.91592159 53.48282547 60.00021299 41.2541072 54.34038759 41.7043665
42.95073848 45.78573569 54.01810105 47.85578105 49.83824907 52.37127885
49.23282526 53.72136749 50.38467915 46.83949837 57.54461243 51.93802136
55.20046982 49.91061841 45.56106189 46.63422067 51.3956761 48.88497186
53.83264266 48.02630316 46.19721282 47.42814485 45.71538531 49.88908503
44.79453141 45.32430345 46.5239075 41.18215201 63.53801071 50.65578603
52.23182537 48.87221857 45.26766042 42.11206657 52.27496116 47.41797743
46.78408864 52.42623906 56.67262474 44.44351944 49.03337102 50.0448144
49.77452736 45.73734096 54.75369321 46.43660445 47.78588839 50.33930367
46.33816024 52.93472189 51.37906554 49.77421034 47.23945969 57.93021055
54.74279953 48.38726861 47.98073074 53.67867507 41.98337754 51.0012706
55.09240508 58.02931757 53.56395843 52.43129637 49.14258002 52.67324599
51.22458474 45.59169153 50.31933579 56.89735679 51.85742521 54.86783714
50.37356662 52.24458867 47.77157503 47.90897777 52.89517737 41.53269064
49.20616079 47.8152969 48.41972233 51.43912227 58.8715616 46.48877938
50.98778582 48.12037201 49.27232232 47.84216009 51.18481192 46.11779067
44.13640585 52.50624214 43.86510837 46.83273076 52.98348712 55.19756728
53.11958228 50.59651367 49.03794304 44.55999983 58.53353129 47.55003221
51.75724092 43.15587509 52.14845383 49.42155991 40.40188657 46.75542867
56.07351097 49.2163863 52.50587188 52.29614613 56.9421125 50.75698477
59.65854844 45.56047263 55.51811931 53.05245033 49.93280933 49.40112759
56.26768607 49.93585134 45.12843268 54.3260334 56.03859378 48.86883295
51.15732685 53.09328023 45.94512531 45.79467343 55.36517235 51.40111115
43.80003294 52.29234798 44.71621776 46.86269184 49.99027541 41.93701126
44.03098978 53.02967007 51.16581519 45.24885877 59.94243231 47.07761551
47.6015467 53.61794671 53.51604863 54.40954224 56.98544589 57.59208077
44.3835447 57.54688801 43.56284599 59.58532397 45.73974721 48.31856116
48.19551933 56.6502933 56.58301301 47.92048493 42.29206002 55.59941441
58.6560349 46.43272302 43.91696516 56.88662681 49.63605113 41.7263654
53.42209464 42.05815866 50.89276651 46.94749639 48.50074993 61.65629158
42.81081178 40.42630649 50.29987854 37.9617346 49.57470954 40.54634135
48.32385937 47.24695864 41.1811045 53.4341088 52.0883548 46.9772072
44.47459455 55.58513873 55.50514888 44.26955973 54.62889869 38.39608897
53.2259724 48.46545051 58.36749718 47.88014838 54.91190969 43.09777722
47.54951024 47.52084792 49.01379734 52.41159163 43.43324092 47.5149727
57.80662581 51.35352351 35.33760897 56.46317867 50.47806926 57.16080148
44.38646343 51.27015622 46.7322984 46.93552991 51.50412507 49.22235939
41.61811126 49.54468836 43.0081292 56.29523835 41.98190936 45.41793994
52.80315694 46.51143233 46.2667349 49.90092411 54.10681407 56.72498841
58.6662943 46.08585193 53.96397826 48.26220971 52.41089283 49.44346475
58.25894266 44.7531947 48.68618378 51.2341902 45.55124029 48.55846629
45.49483852 51.27703514 44.43381143 59.68871382 48.07284373 55.6747159
55.99107668 46.42729656 49.80576878 55.81275905 45.34852087 46.27504294
45.60223675 47.47150468 45.16846129 53.5511301 54.2715687 45.70333547
53.09093617 50.45861208 48.67533055 49.19207367 48.88167339 46.70177437
41.62132092 52.2805185 48.83012954 53.99329914 48.99568062 41.66333436
49.3119263 52.5195989 52.34409192 46.04405085 53.14679012 45.20308887
53.03215348 42.58558974 44.00418778 53.39328688 42.4512753 42.79965226
48.1519196 51.14674227 49.19339047 52.52044397 51.67231714 56.93221559
51.61996312 51.02373292 51.04131428 51.83657467 51.17349796 44.85169668
62.42707349 53.64122926 47.43320533 44.31987208 53.06590081 47.05252291
59.06973682 43.67175254 56.76253805 46.91019631 48.75893403 57.1524513
47.36593025 55.36446725 46.47811537 45.26226077 53.48849787 54.51353545
45.93761917 46.91802797 59.24184782 52.8002112 50.04654473 52.31107515
49.73789583 49.0898491 50.62060678 49.80476319 49.25278719 53.26259409
49.73716194 40.41324176 55.59714128 45.00468757 52.65537364 55.15070652
59.04494951 45.89013901 55.25956633 49.06078377 53.76481963 52.18002093
46.403185 48.01796805 46.92041563 50.16084128 53.90516395 51.76160683
55.24569002 42.81420432 51.7907904 52.85447648 56.24183494 47.96286257
48.7831745 54.39002991 47.52973421 52.46085607 45.94108366 43.35832344
50.9264486 52.81026464 46.58557557 45.28926192 58.59412489 45.75227125
63.29301197 63.72284706 39.70912597 45.59268926 43.20621396 53.77757329
61.16524396 41.50436687 52.71383468 47.82052199 49.15834333 42.66695836
48.42162577 47.64899966 46.64136801 53.11862026 58.72391264 55.29394736
43.50530758 62.4342982 47.97902593 46.58359212 42.24945788 46.80192342
48.39942284 47.88610234 54.8415123 49.93687934 44.0921694 47.86248491
46.78932452 44.1566366 49.86213323 50.91700543 54.02568668 45.82182714
48.90484622 49.18668293 51.52637599 45.62854081 50.57624709 48.47041471
46.32795659 47.55624621 45.30573101 51.12178421 55.13259069 46.78273488
45.61432342 43.10047054 43.84326302 63.94977241 48.23879819 36.34688186
45.55382221 44.19358595 43.18331732 48.69737072 48.87007819 52.55270555
45.923502 42.02065483 49.99996874 45.32998864 48.18392569 40.89165153
52.5039153 53.33728444 43.05643874 47.70461933 52.47659077 46.87364044
45.81395589 57.87728645 59.5911764 41.98449809 53.0445828 45.84246188
55.67103462 44.82997483 49.46826464 59.04989726 39.74931295 46.75450918
47.36751985 47.56896006 53.37951999 49.12685533 51.29937791 58.1195888
49.26004145 51.41073561 51.1600571 59.11924114 53.79005436 53.85103373
58.68938748 45.28327154 55.56857031 55.42645891 59.28774509 49.1094993
47.90061956 55.1238636 53.11475674 46.64112674 42.88448218 50.59567884
53.26310632 45.46907667 38.79342566 50.74867767 48.48515417 49.12341738
55.43892107 51.76396988 48.4032712 57.71901787 49.10976492 50.44455668
47.60661611 57.33431029 51.31226425 50.85466596 46.61474904 51.77943192
58.39274448 39.4372268 56.10754645 46.24580382 56.36877976 50.79792728
42.72990353 50.16340872 51.01807193 51.50900501 61.23797959 45.93737896
56.4108352 51.30896823 46.85447245 51.92650036 57.66723624 43.85424447
53.08673015 51.72562937 59.41696794 50.45688642 56.03801566 42.87704024
49.8072696 49.80680536 48.63802684 46.7531589 40.32766679 47.40482966
57.40498325 55.83851175 39.53651072 46.64357579 50.70438457 52.38526869
43.12165469 50.26743042 52.1396014 45.33439687 49.03330222 51.12884438
44.12413276 49.15585292 47.92949262 51.55941427 43.60861699 52.23288868
46.21697759 45.49072643 49.4592697 45.34457715 45.67817924 52.98036288
45.02722283 55.13942431 48.52092745 58.17278521 53.90670753 48.08682891
49.70113771 50.10693593 52.64372474 45.8642834 47.67159953 51.1407116
59.58798777 43.82037915 45.0399636 52.75628444 41.47326863 49.43993315
47.06111085 49.32405587 45.89933917 49.05930655 56.35533378 52.69292305
38.89461451 56.1876302 54.25331124 54.98528928 50.99422698 43.5677607
53.55552709 46.28679632 56.07084505 55.01253813 60.17580106 50.77904143
57.01217507 49.66225433 56.15037069 47.67251908 56.15311637 46.42874307
55.89683115 49.46144326 49.43672273 55.64144384 47.11655741 56.96592619
50.1866608 57.4248059 48.64703041 54.31873196 49.98451562 58.78792844
56.08847687 43.59728897 55.94177789 54.33123766 40.83823925 56.07110701
54.01785181 50.9880377 40.51923968 46.53839013 53.60669856 47.88303711
48.87044558 49.77823048 50.89660784 56.4106294 51.30856964 50.21505016
50.83069032 51.88833404 44.30221916 59.60196782 60.12336003 45.97643967
49.40085743 55.06075086 52.19148683 50.09595171 58.0122757 46.86647066
42.30143838 46.02415552 49.71170975 53.27866693 54.37751463 41.12248349
48.04351728 53.56354414 53.92826001 52.31124653 57.91319199 52.45151936
43.98567275 55.5747765 50.33169874 50.52903373 52.827833 41.48511958
51.25748627 48.92860217 57.85397731 58.51592309 46.02013682 53.59767699
35.41421717 41.42813354 48.974475 49.79004892 53.1628669 46.57971496
49.66892843 47.56305904 50.43399135 51.07657835 46.87751698 42.23855693
53.4479218 50.72058823 49.02085332 55.06466686 47.47358542 54.40051716
47.96423186 41.39509886 59.05080511 52.53748755 48.06431687 41.82915017
51.06623632 54.80408155 53.24604813 51.13363745 42.48596447 52.68633282
48.32572603 51.17846693 49.32300907 55.26935009 48.87868711 45.28153561
49.66231664 52.18694843 48.75499511 38.77365063 45.19356684 36.50451429
47.80164587 49.34802874 52.96974873 51.68894492 49.51427388 44.82147179
49.6017693 56.13401603 55.64302952 53.62024822 52.41733344 50.39173524
48.69573067 51.55153927 49.88707159 49.2425006 39.76813097 43.42245013
53.53472061 46.62689054 47.38056701 55.09554132 48.58873085 48.82803004
46.98163154 55.43279499 51.89806455 54.55049823 46.99366681 45.10403634
50.76216904 47.61074163 45.79959911 45.34551882 47.20408287 51.43838831
50.2009296 56.28313225 52.99732445 46.0034802 47.47706472 49.82681892
47.04789874 56.22580255 51.97285156 60.79054678 45.31275132 54.34863973
49.12829679 51.91157525 58.75234161 49.19557032 46.12312554 45.98055012
48.99864853 50.25042575 57.82002781 53.17043568 44.14580354 48.54472463
58.20639172 55.67841483 56.59873382 56.65179835 48.8852473 52.1800937
49.94308274 42.90249753 52.14879712 42.67975754 46.89890983 50.7933895
51.62488242 53.70937395 63.32526536 47.51620939]
# 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
# import random and set seed
import random
s = np.random.seed(123456)
# Simulate drawing a single ball; each ball has a number from from 1 to 50
b = np.random.randint(low = 1, high = 50)
# Simulate drawing 100 balls and keep track of the number of balls
# that have a number less than 25
b = np.random.randint(low = 1, high = 50, size = 100)
a = 25
k = 0
for i in b:
if i < a:
k = k + 1
print(f"Balls less than 25 are: {k}")
Balls less than 25 are: 48
# Calculate discrete probability that you will draw a ball with a number <25 using 10,000
# simulations
# 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 Ball():
x = np.random.randint(low = 1, high = 50)
if x < 25:
print('True')
elif x >= 25:
print("False")
print(x)
Ball()
False
27