掷骰子模拟
我必须编写一个脚本,使用用户输入来掷一定数量的骰子、一定数量的面、一定数量的骰子和一定数量的试验。
我已经掌握了整个用户输入部分,但我在编写掷骰子的函数时遇到了麻烦。
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
我该如何编写代码才能将试验和滚动的数量考虑在内?我知道我可能必须用 for 循环做一些事情,但我很困惑,目前什么也想不起来。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ResultMatrix = randi(S,N,R,T)
这为每个试验创建一组“T”矩阵(第一个矩阵是第一个试验,依此类推),每个矩阵都有“R”列用于每次滚动(第 1 列是滚动) 1 等),每个骰子掷出“N”行(第 1 行是骰子 1 等)。当然,这些值从 1:S 开始,代表掷骰的结果。
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.