2011-03-10 125 views
-1

我必須編寫一個腳本,它使用用戶輸入來滾動一定數量的骰子,一定數量的骰子,一定量的骰子和一定數量的試驗。滾動骰子模擬

我有整個用戶輸入部分,但我無法編寫滾動骰子的函數。

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 

我該如何編寫代碼,以便能夠計算試用和滾動的數量?我知道我可能不得不爲循環做些事情,但我很困惑,現在我什麼也想不到。

+1

這是功課嗎?如果是這樣,你可能會重新標記你的問題。謝謝 – eat 2011-03-10 08:44:13

+1

你可能想看看[現有的代碼](http://www.mathworks.com/matlabcentral/fileexchange/27327-a-dice-roller-for-matlab)骰子滾筒。 – Jonas 2011-03-10 14:43:06

回答

1

ResultMatrix =蘭迪(S,N,R,T)

這對於每個試驗創建一組的 「T」 矩陣(第一矩陣是所述第一試驗中,等),每個具有 「R」 的列對於每個卷(第1列是卷1等)以及每個擲出的骰子(第1行是第1個等)的「N」行。當然,這些值從1:S開始,代表了滾動的結果。