
%%%Written by Natasha Komarov, Dartmouth College.
%%%Use this program as you see fit.
%%%I make no guarantees that anything will work as advertised.


function DiceRolls(num_rolls,num_dice)
%DiceRolls gives the results of #num_rolls tosses 
%of #num_dice 6-sided dice. Results are plotted in a histogram.


        
    %%%%%%%%%%% POSSIBLE METHOD 1 %%%%%%%%%%%%%%%%
    rolls = zeros(1,num_rolls); %create a vector that'll hold our results
        %zeros(n,m) makes an nxm matrix full of 0's
        %semicolon at the end suppresses the output
     
    for i=1:length(rolls)
        rolls(i) = sum(randi(6,1,num_dice));
        %randi(a,b,c) gives a bxc matrix of random integers from 1 to a
        %sum(M) gives the sum of all entries in the matrix M
    end
        %put the sum of the num_dice random integers in each cell of rolls
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
        
        
    
    % if you're uncomfortable with matlab, just stick with method 1 for
    % now.  if you're feeling curious, check out method 2 for a neater
    % way of encoding the same information
    
    
    
    
    
    
    
    
    
    
    
        
    %%%%%%%%%%% POSSIBLE METHOD 2 %%%%%%%%%%%%%%%%
    rolls = zeros(1,num_rolls); %create a vector that'll hold our results
        %zeros(n,m) makes an nxm matrix full of 0's
        %semicolon at the end suppresses the output


    rolls = arrayfun(@(x) sum(randi(6,1,num_dice)), rolls);
        %arrayfun(f,M) applies the function f to each cell of the
        %matrix M
        %our function here takes in the index of the cell (x) and assigns 
        %to it sum(randi(6,1,num_dice)) [note: in this case, the function
        %doesn't depend on x]
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
   
        
        
    %We now have a list of length #num_rolls that contains the results
    %of each of the rolls of the dice

    hist(rolls) %plot the list in a histogram

        
        
        
        
    %%%%%%%%%%% GENERAL NOTE ABOUT ARRAYFUN %%%%%%%%%%%%%%%%
        %arrayfun(f,M) is the same as doing a for loop which iteratively
        %does the function f on each cell of M
        %arrayfun runs faster than a for loop, but both methods are fine
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
   
    
 
    
    
end


