% Use a uniform distribution to choose students at random % We will generate code for sampling students at random % There are ? students... % we divide the space between 0 and 1 into equal intervals and mark them names=['Antonio ' 'Carlos ' 'Carolina ' 'CristianeC' 'CristianeJ' 'Bia ' 'Maria ' 'Rosa ' 'MauricioB ' 'Rita ' 'Talita ' 'Daniela ' 'MauricioC ' 'Vania ' 'Flavia ' 'Luciano ' 'Silvana ']; dimnames=size(names); %dimensions of names matrix nnames=dimnames(1); %no. of rows in names matrix, what does this tell us? marks=linspace(0,1,nnames+1); %we want n+1 marks from 0 to 1 marks1=marks(1:nnames); %start with 0, end before 1 marks1=marks1'; matches=[names num2str(marks1)]; %turn the numerical marks into character "strings" matches %learn your interval... this is the lower bound of it %imagine that the 20 random numbers are drawn... % who will be called on? T=20 %length of loop, %try this again with 200, and then with 2000 rand('state',sum(100*clock)) %reseeds the random number generator %loop to choose random numbers and set up matrix to receive chosen students for i=1:T X = rand(1,T); students= char(zeros(i,dimnames(2))); % make it a character array i rows, same length as "names" end; %loop to choose students for i=1:T; j = min(find(marks > X(i))); %get ith random number and decide which student it corresponds to % find the _first_ time along the line a particular mark is greater than the random number student = names(j-1,:); % go back one: that is the student whose segment it is students(i,:)=student; %put the student into the sequence of chosen ones end; students % here is the sequence chosen at random %make a plot of the frequency distribution of chosen ones spacesshift=marks(2:nnames+1); %find midpoints step 1 midspaces=(marks(1:nnames)+spacesshift)/2; %find midpoints step 2 n=hist(X(2:T),midspaces) %for T don't include the first element which is 0, number of bins equals number of students figure(1) subplot(2,1,1) bar(n) subplot(2,1,2) hist(X(2:T),midspaces) %for T don't include the first element which is 0, number of bins equals number of students axis([0 1 0 max(n+1)]) % set the y-axis according the maximum observed frequency xvalues=names; set(gca,'XTick',[midspaces],'FontName','Helvetica','FontSize',6) set(gca,'XTickLabel',{xvalues},'FontName','Helvetica','FontSize',6)