掷骰子模拟

发布于 2024-10-21 07:51:38 字数 681 浏览 1 评论 0原文

我必须编写一个脚本,使用用户输入来掷一定数量的骰子、一定数量的面、一定数量的骰子和一定数量的试验。

我已经掌握了整个用户输入部分,但我在编写掷骰子的函数时遇到了麻烦。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-10-28 07:51:38

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文