MATLAB Help - Rolling Dice Simulation

117 views (last 30 days)
Aaron
Aaron on 10 Mar 2011
I have to write a script that uses the user input to roll a certain amount of dice, with a certain amount of sides, with a certain amount of rolls , and a certain amount of trials.
I have the whole user input part down, but I'm having trouble writing a function for rolling the dice.
function [ X ] = Dice( N, S, T, R ) % Dice simulates a random selection of numbers which is similar to how a % dice is rolled % % N is the number of dice the user wants to roll % S is the number of sides on the dice % T is the number of trials that the user wants to run. % R is the number of rolls that the user wants to roll each dice.
D =ceil(S*rand(1,N))
% I used this for one roll of the dice
Counts = hist(D,[1:S]);
% Then I used this to count how many of each number showed up
How do I write the code so that I can factor in the amount of trials and rolls? I know I probably have to do something with for loops, but I'm very confused and I can't think of anything at the moment.

Answers (1)

Oleg Komarov
Oleg Komarov on 10 Mar 2011
S = 6; % six faces
R = 3; % each dice is rolled 3 times
N = 10; % roll 10 dice at once
T = 2; % repeat all 2 times
Out = randi([1 S],[R N T])
Out(:,:,1) =
5 6 2 6 6 1 5 1 5 3
6 4 4 1 3 3 6 6 5 4
1 1 6 6 5 6 4 6 5 2
Out(:,:,2) =
5 1 5 1 5 3 5 5 1 3
1 1 2 3 5 3 5 4 3 4
2 5 6 3 2 4 2 1 6 2
Each slice (on the third dimension) is a trial.
Each column is a dice rolled 3 times.
Oleg

Categories

Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!