% roll dice example % uses only script files % Variables: rolls = 0; % rolls so far MINSIZE = 4; % min size of a die MAXSIZE = 100; % max size of a die dieSize = 0; % current size of die (not chosen yet) counts = []; % counts of each die roll so far (nothing) % Prompt user to pick die size between MINSIZE and MAXSIZE: dieSize = str2double(input('Enter die size: ','s')); % get initial size while ~isreal(dieSize) | ... % no complex numbers isinf(dieSize) | ... % no infinite sizes isnan(dieSize) | ... % no NaNs ~isnumeric(dieSize) | ... % no non-numerical sizes floor(dieSize)~=ceil(dieSize) | ... % no decimal/fraction sizes dieSize < MINSIZE | ... % lower bound of size dieSize > MAXSIZE % upper bound of size disp('That size is not a valid! Please re-enter: '); dieSize = str2double(input('Enter die size: ','s')); end % Prompt user to pick how many rolls to do: rolls = str2double(input('Enter rolls to do: ','s')); % get initial rolls while ~isreal(rolls) | ... % no complex numbers isinf(rolls) | ... % no infinite rolls isnan(rolls) | ... % no NaNs ~isnumeric(rolls) | ... % no non-numerical rolls floor(rolls)~=ceil(rolls) | ... % no decimal/fraction rolls rolls > realmax | ... % lower bound of size rolls <= 0 % upper bound of size disp('That amount of rolls is not a valid! Please re-enter: '); rolls = str2double(input('Enter rolls to do: ','s')); end % Create array of die rolls: counts = zeros(dieSize,1); % Roll the die and count how often each side appears: for currentRoll=0:rolls dieRoll = floor(rand*dieSize) + 1; % roll die counts(dieRoll) = counts(dieRoll)+1; % add that roll to its count end % Report the frequences: 100*counts/rolls