开发者

Rolling Dice Simulation

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 side开发者_运维问答s 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.


ResultMatrix = randi(S,N,R,T)

This creates a set of "T" matrices for each trial (the first matrix is the first trial, etc), each with "R" columns for each roll (column 1 is roll 1, etc) and "N" rows for each dice rolled (row 1 is die 1, etc). The values, of course, go from 1:S and represent the result of the roll.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜