Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:35:30+00:00 2026-06-14T15:35:30+00:00

My function called RollDice simulates the rolling of a given number of six sided

  • 0

My function called RollDice simulates the rolling of a given number of six sided dice a given number of times. The function has two input arguments, the number of dice (NumDice) that will be rolled in each experiment and the total number (NumRolls) of times that the dice will be rolled. The output of the function will be a vector SumDice of length NumRolls that contains the sum of the dice values in each experiment.

This is my code right now: how do I account for the SUM of the dice? Thanks!

function SumDice= RollDice(NumDice,NumRolls)

FACES= 6;               
maxOut= FACES*NumDice;         
count= zeros(1,maxOut);  


for i = 1:NumRolls

    outcome= 0;  
    for k= 1:NumDice
        outcome= outcome + ceil(ranNumDice(1)*FACES);
    end

    count(outcome)= count(outcome) + 1;
end


bar(NumDice:maxOut, count(NumDice:length(count)));
message= sprintf('% NumDice rolls of % NumDice fair dice',  NumRolls, NumDice);
title(message);
xlabel('sum of dice values');  ylabel('Count');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:35:31+00:00Added an answer on June 14, 2026 at 3:35 pm

    This is a simple and neat little problem (+1) and I enjoyed looking into it 🙂

    There were quite a few areas where I felt I could improve on your function. Rather than going through them one by one, I thought I’d just re-write the function how I’d do it, and we could go from there. I’ve written it as a script, but it can be turned into a function easily enough. Finally, I also generalized it a bit by allowing for the dice to have any number of faces (6 or otherwise). So, here it is:

    %#Define the parameters
    NumDice = 2;
    NumFace = 6;
    NumRoll = 50;
    
    %#Generate the rolls and obtain the sum of the rolls
    AllRoll = randi(NumFace, NumRoll, NumDice);
    SumRoll = sum(AllRoll, 2);
    
    %#Determine the bins for the histogram
    Bins = (NumDice:NumFace * NumDice)';
    
    %#Build the histogram
    hist(SumRoll, Bins);
    title(sprintf('Histogram generated from %d rolls of %d %d-sided dice', NumRoll, NumDice, NumFace));
    xlabel(sprintf('Sum of %d dice', NumDice));
    ylabel('Count');
    

    I suggest you have a close look at my code and the documentation for each function I’ve used. The exercise may prove useful for you when tackling other problems in Matlab in the future. Once you’ve done that, if there is anything you don’t understand, then please let me know in a comment and I’ll try to help. Cheers!

    ps, If you don’t ever need to refer to the individual rolls again, you can of course convert the AllRoll and SumRoll line into a one-liner, ie: SumRoll = sum(randi(NumFace, NumRoll, NumDice), 2);. I think the two-liner is more readable personally, and I doubt it will make much difference to the efficiency of the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My function called DicePlot, simulates rolling 10 dice 5000 times. In the function, it
I have a SQL function called get_forecast_history(integer,integer) that takes two arguments, a month and
Say I have a function called addUp which takes any number of decimal arguments
I use a function called count_days($date1,$date2) that counts the number of days between two
Django has a decorator function called @transaction.commit_manually . I am attempting to pass a
I have a function called Colorbox (jQuery plugin) that takes a number of parameters
Jquery has utility a function called ' extend ' which can be used to
I have a Javascript function called many times from many pages. Inside this function
PHP has a great function called htmlspecialcharacters() where you pass it a string and
PostgreSQL has a useful function called GREATEST . It returns the largest value of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.