%DICE Simulate fair dice % % dice rolls once a 6-sided die % dice(n) rolls n times a 6-sided die % dice(n,d) rolls n times a d-sided die % function x=dice(n,d) % Default values if nargin<1, n=1; end if nargin<2, d=6; end % Take n random numbers from the uniform distribution of (0,1). y = rand(1,n); % Multiply by d, so the numbers are in interval (0,d). % Round up to integer, so then they are integers in {1,...,d}. x = ceil(y * d);