python11(): Exercises for random numbers, simulation, testing. Initialize random number generator with seed = 123456789 Five random integers 0 <= i < 100: [90 2 58 90 34 88 29 62 12 79] Initialize random number generator with seed = 987654321 Five random integers 0 <= i < 100: [81 28 77 17 92 19 25 36 83 14] Initialize random number generator with seed = 123456789 Five random integers 0 <= i < 100: [90 2 58 90 34 88 29 62 12 79] r = rng.integers ( low = 0, high = 100 ) 19 r = rng.integers ( low = 0, high = 100, size = 1 ) [82] r = rng.integers ( low = 0, high = 100, size = 3 ) [41 84 6] r = rng.integers ( low = 0, high = 100, size = ( 3, 2 ) ) [[47 24] [95 82] [94 65]] Return random floats in [0,1] r = rng.random ( ) 0.08364013148965865 rng.random ( 1 ) [0.26984426] rng.random ( 3 ) [0.05379984 0.56661849 0.01891774] rng.random ( ( 3, 2 ) ) [[0.20537362 0.24935483] [0.89877802 0.10779729] [0.74867811 0.71470362]] statistics for random(): x = rng._random ( 10000 ) x[0:5] = [0.74077908 0.94511748 0.45946087 0.23163497 0.46497377] mean ( x ) = 0.5024079988122446 Distribution mean = 0 std ( x ) = 0.2887575177771944 Distribution std = 0.2886751345948129 min ( x ) = 2.084883961028794e-05 Distribution min = 0.0 min ( x ) = 0.9999101448053969 Distribution max = 1.0 Return random floats in [ 3.141592653589793 , 10.0 ] r = a + ( b - a ) * rng.random ( ) 9.543135325260273 r = a + ( b - a ) * rng.random ( 1 ) [5.31041253] r = a + ( b - a ) * rng.random ( 3 ) [6.87005209 3.83932539 9.22829008] r = a + ( b - a ) * rng.random ( ( 3, 2 ) ) [[6.98204939 6.18873156] [5.82288157 7.94788268] [7.74022727 3.41641799]] r = rng.standard_normal ( ) 0.6227622538674881 rng.standard_normal ( 1 ) [-1.25521346] rng.standard_normal ( 3 ) [-0.36810849 0.1818789 1.5069514 ] rng.standard_normal ( ( 3, 2 ) ) [[0.62958443 0.1031141 ] [1.07140017 1.60669513] [1.35884452 1.48410916]] standard_normal: x = rng._standard_normal ( 10000 ) x[0:5] = [ 0.21711346 -0.25249231 -0.38976507 0.18114662 -1.50870976] mean ( x ) = -0.005161080790342771 Distribution mean = 0.0 std ( x ) = 1.0094860025727166 Distribution std = 1.0 min ( x ) = -4.234554911424464 Distribution min = -inf max ( x ) = 4.055359783888028 Distribution max = +inf normal: x = rng.normal ( 10.0 , 2.0 , 1000 ) x[0:5] = [10.3329046 9.75213545 10.78131338 10.44510029 7.12891626] mean ( x ) = 10.000603438014656 Distribution mean = 10.0 std ( x ) = 2.0154419861609827 Distribution std = 2.0 min ( x ) = 1.5983534388648746 Distribution min = -inf max ( x ) = 17.15938314341716 Distribution max = +inf Randomly permute a vector. a = np.arange ( 10 ) [0 1 2 3 4 5 6 7 8 9] b = np.permutation ( a ) [9 8 1 2 5 4 7 3 6 0] Randomly permute a matrix. c: [[ 0 1 2 3 4] [10 11 12 13 14] [20 21 22 23 24]] d = rng.permutation ( c ) [[20 21 22 23 24] [10 11 12 13 14] [ 0 1 2 3 4]] e = np.transpose ( d ) [[20 10 0] [21 11 1] [22 12 2] [23 13 3] [24 14 4]] f = rng.permutation ( e ) [[21 11 1] [22 12 2] [23 13 3] [24 14 4] [20 10 0]] g = rng.transpose ( e ) [[21 22 23 24 20] [11 12 13 14 10] [ 1 2 3 4 0]] defg = np.transpose ( rng.permutation ( np.transpose ( rng.permutation ( c ) ) ) ) [[22 20 21 23 24] [12 10 11 13 14] [ 2 0 1 3 4]] Randomly select 3 items from 12, using permutation. ['Jun' 'Feb' 'Apr'] Randomly select 5 cards from 52, no repeats cards = np.arange ( 52 ) [ 0 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] b = rng.permutation ( cards ) [33 4 41 40 37 29 44 17 3 15 35 32 51 31 34 50 23 7 46 11 1 8 16 25 45 5 21 18 48 28 49 24 30 19 27 14 9 47 42 38 2 26 43 20 10 13 39 12 0 36 6 22] c = b[0:5] [33 4 41 40 37] Randomly select 5 cards from 52, can have repeats cards = np.arange ( 52 ) [ 0 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] b = rng.choice ( cards, size = 5 ) [47 39 2 37 14] Randomly select W/L/T values for a game season winlosstie = [ 'win', 'loss', 'tie' ] ['win', 'loss', 'tie'] record = rng.choice ( winlosstie, size = 10 ) ['loss' 'loss' 'loss' 'win' 'win' 'win' 'tie' 'tie' 'loss' 'loss'] record = rng.choice ( winlosstie, size = 10 ) ['win' 'tie' 'win' 'tie' 'win' 'tie' 'tie' 'loss' 'loss' 'tie'] Generate a binary matrix. binary = [ 0, 1 ] [0, 1] binary_matrix = rng.choice ( binary, size = ( 4, 5 ) ) [[0 0 0 1 1] [1 0 0 1 1] [1 1 0 0 0] [1 0 1 0 0]] Compute estimate Q for integral(a<=x<=b) f(x) dx N Q Error 10 0.8392960364153677 0.07626788466784307 100 0.7017843825644043 0.061243769183120245 1000 0.7652918738910668 0.0022637221435422017 Compute estimate Q for area of ellipse N Q Error 10 8.0 1.7168146928204138 100 6.16 0.12318530717958609 1000 6.264 0.019185307179585998 Estimate average distance between two points in unit square. N Estimate Error 10 0.5670205557895164 0.04561512262479572 100 0.5063219136715147 0.015083519493205966 1000 0.5148028843337755 0.006602548830945132 10000 0.5231199707619855 0.0017145375972648091 ||A||2 <= ||A||F always? Statement was true 1000 times out of 1000