% CS99 Fall 2002 % Prelim 1 % Problem 2 %------------------------------------------------------------------------------ % 2a Algorithm: % Set number of rolls so far to zero. % Set number of snake-eyes so far to zero. % If count of rolls is not exceeded (100), roll both dice. % + If dice1 and dice2 rolled 1, increment count of snake-eyes. % + Increment count of rolls. % + Repeat. % Report the number of snake-eyes out of 100 rolls. % % 2b Solution: % Initialize variables: maxrolls = 100; % max number of allowed rolls count = 0; % number of rolls so far snakeeyes = 0; % number of snake-eyes so far % Roll both dice and count snakeeyes for maxrolls: for count=1:maxrolls count=count+1; roll1=floor(rand*6)+1; % roll 1st die roll2=floor(rand*6)+1; % roll 2nd die % Count snake-eyes: if roll1==1 & roll2==1 snakeeyes=snakeeyes+1; end end % Report chance of rolling snake-eyes disp(['Chance of rolling snake-eyes: ',num2str(100*snakeeyes/maxrolls),'%']);