在MATLAB中总结骰子 [英] Summing up Dice in MATLAB

查看:450
本文介绍了在MATLAB中总结骰子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的函数RollDice模拟给定次数的给定数量的六个侧面骰子的滚动.该函数有两个输入参数,每个实验将掷骰子的数量(NumDice),以及掷骰子的总次数(NumRolls).函数的输出将是长度为NumRolls的向量SumDice,其中包含每个实验中骰子值的总和.

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),我喜欢研究它:-)

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

我认为我可以在很多方面改善您的功能.与其一一遍地研究它们,我想我只是按照自己的方式来重写该函数,我们可以从那里开始.我已经将其编写为脚本,但是可以很容易地将其转换为功能.最后,我还通过允许骰子具有任意数量的面孔(6个或其他形式)来对它进行了概括.因此,这里是:

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');

我建议您仔细查看我的代码和我所使用的每个功能的文档.在将来解决Matlab中的其他问题时,该练习可能对您很有用.完成此操作后,如果您不了解任何内容,请在评论中让我知道,我将尽力提供帮助.干杯!

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,如果您不再需要再次引用各个卷,则可以将AllRollSumRoll行转换为单行格式,即:SumRoll = sum(randi(NumFace, NumRoll, NumDice), 2);.我认为两层结构个人可读性更高,我怀疑这将对代码的效率产生很大影响.

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.

这篇关于在MATLAB中总结骰子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆