Chapter 2: speeding your simulations
Chapter 2: speeding your simulations
As a programming side-note, we can increase the speed of this simulation by eliminating the for-loops and replace them instead with multiple draws from RANDI:
D=randi(Nballs,[REPS 3]);
where the above logical expression again evaluates whether the draws meet our criterion and approximates the probability calculation. Or, using a slightly different logical expression to evaluate the draws, the same could have been obtained from RAND instead of RANDI:
D=rand(REPS,3);
sum(or(or(...
and(D(:,1)>74/Nballs,and(D(:,2)<=74/Nballs,D(:,3)<=74/Nballs)),...
and(D(:,2)>74/Nballs,and(D(:,1)<=74/Nballs,D(:,3)<=74/Nballs))),...
and(D(:,3)>74/Nballs,and(D(:,1)<=74/Nballs,D(:,2)<=74/Nballs)))...
)/REPS