% do the following several times: rand(1) % see that values returned are between 0-1 % do the following several times: rand(1)*100 % see that values are between 0-100 % Ask about the average (50) % How do you get a random number between 50-150 with average 100? rand(1)*100+50 % show the following: mean = 50; x = zeros(1,10000); for i=1:10000 x(i) = ceil(rand(1)*2*mean); end % ask on the average how many random numbers are 10, 11,..? % 10000 total numbers and 100 integer values they can take so answer is 100 % on the average % ask do you expect that the average is the same for all numbers? The number % of times the random value 10 is picked is about the same as the number of % times the random value 12 is picked? % design an experiment to find out. (exp_1) mean = 50; npts = 10000; x = zeros(1,npts); for i=1:npts x(i) = ceil(rand(1)*2*mean); end v = zeros(1,2*mean); for i=1:npts v(x(i)) = v(x(i)) +1; end plot(v); axis([0 2*mean 0 1.2*max(v)]); % A question about means: If I have 100 variables of mean 1, what is the mean % of the sum of the variables? % Try an experiment: (exp_2) mean = 1; npts = 100; nexp = 100; v = zeros(1,npts); for i=1:nexp x = rand(1,npts)*2*mean; v(i) = sum(x); end plot(v); axis([0 nexp 0 1.2*npts*mean]); % Answer: it is the sum of the means of the variables. % Well maybe this does not work if the variables have different means % try an experiment where means are increasing (exp_3) mean = 1; npts = 1000; nexp = 1000; v = zeros(1,nexp); for i=1:nexp x = zeros(1,npts); for j=1:npts x(j) = rand(1)*2*j*mean; end v(i) = sum(x); end plot(v); axis([0 nexp 0 0.6*npts*npts*mean]); sum(v)/nexp % the average % That variance is troubling - let's define it: % var = average of (X-mean)^2 % var of uniform distribution: (exp_4) mean = 50; npts = 100000; x = zeros(1,npts); for i=1:npts x(i) = (rand(1)*2*mean - mean)^2; end printf('var= %f std. dev.= %f\n',sum(x)/npts, sqrt(sum(x)/npts)); % variance of the other distribution (exp_5) mean = .001; npts = 1000; nexp = 1000; x = zeros(1,nexp); y = zeros(1,nexp); for k=1:nexp for i=1:npts x(k) = x(k) + 2*i*mean; end y(k) = (x(k) - npts*mean)^2; end printf('mean= %f var= %f std. dev.= %f\n',sum(x)/nexp, sum(y)/nexp, sqrt(sum(y)/nexp)); % Normal distribution randn(1) % Find the mean (exp_6) nexp = 1000; x = zeros(1,nexp); for i=1:nexp x(i) = randn(1); end printf('mean= %f variance= %f std.dev.= %f\n', sum(x)/nexp, sum((x-sum(x)/nexp)^2), sqrt(sum((x-sum(x)/nexp)^2))); % Experiment with the standard deviation - use exp_6 s*randn(1) % Plot (exp_7) % Show that sum of random vars is gaussian with mean = sum of means and % std dev = sum of std dev. (exp_8) %% Cells a{1} = [1 2 3] a{2} = [4 5] a{3} = [6 7 8 9 10] a l = a{1} %% Make rounds % Returns a cell array where each cell is the outcome of dice rolls for a round function rnds = rounds(n); point = 0; for i=1:n % n rounds % come out roll roll = ceil(rand(1)*6) + ceil(rand(1)*6); tmp = [roll]; if roll == 4 | roll == 5 | roll == 6 | roll == 8 | roll == 9 | roll == 10 point = roll; roll = ceil(rand(1)*6) + ceil(rand(1)*6); tmp = [tmp roll]; while roll ~= point & roll ~= 7 roll = ceil(rand(1)*6) + ceil(rand(1)*6); tmp = [tmp roll]; end end rnds{i} = tmp; end end