function hw5solutions
clear all
n=1000000;
nums=1+6*rand(n,1);
avg=mean(nums);
sqrtavg=mean(sqrt(nums));
sqravg=mean(nums.^2);
invavg=mean(1./nums);
fprintf('The expected val of sqrt(x) is %f\n',sqrtavg)
fprintf('The expected val of x^2 is %f\n',sqravg)
fprintf('The expected val of 1/x is %f\n',invavg)
hist(nums,50)
title('Histogram of Uniform Data')
figure
hist(sqrt(nums),50)
title('Histogram of sqrt of Uniform Data')
figure
hist(nums.^2,50)
title('Histogram of square of Uniform Data')
figure
hist(1./nums,50)
title('Histogram of inverse of Uniform Data')
disp('press any key to continue')
pause

nums=4 +randn(n,1);
avg=mean(nums);
sqrtavg=mean(sqrt(nums));
sqravg=mean(nums.^2);
invavg=mean(1./nums);
fprintf('The expected val of sqrt(x) is %f\n',sqrtavg)
fprintf('The expected val of x^2 is %f\n',sqravg)
fprintf('The expected val of 1/x is %f\n',invavg)
figure
hist(nums,50)
title('Histogram of Normally Distributed Data')
figure
hist(sqrt(nums),50)
title('Histogram of sqrt of Normally Distributed Data')
figure
hist(nums.^2,50)
title('Histogram of square of Normally Distributed Data')
figure
hist(1./nums,50)
title('Histogram of inverse of Normally Distributed Data')
disp('press any key to continue')
pause

n=1000000;
roll1=ceil(6*rand(n,1));
roll2=ceil(6*rand(n,1));
tot=roll1+roll2;
sevens=numel(find(tot==7));
prob=sevens/n;
fprintf('The probability of rolling a 7 is %f\n',prob)
disp('press any key to continue')
pause

clear all
nsamples=100000;
bust=0;
notbust=0;
for i=1:nsamples
    c=randperm(52);
    points=value(c);
    aces=find(points==1);
    points(aces)=points(aces)+10;
    hand=points(1)+points(3);
    dealer=points(2)+points(4);
    if hand==15 && points(4)==10
        hand=hand+points(5);
        if hand>21
            bust=bust+1;
        else
            notbust=notbust+1;
        end
    end
end
fprintf('Fraction meeting criteria is %f\n', (bust+notbust)/nsamples)
fprintf('Bust Fraction is %f\n',bust/(bust+notbust))
%
function v = value(x)
% Evaluate hand
v = mod(x-1,13)+1;
v = min(v,10);