flip coins a given number of times

3 ビュー (過去 30 日間)
Francesco L
Francesco L 2016 年 2 月 28 日
回答済み: Hristo Zhivomirov 2020 年 5 月 3 日
Hi, I have to write a code to flip 3 coins a given number of times. Someone can help me? Thank you

回答 (3 件)

Muhammad Usman Saleem
Muhammad Usman Saleem 2016 年 2 月 28 日
may make a code about probability of appearing head or tail in all flip

Image Analyst
Image Analyst 2016 年 2 月 28 日
Use inputdlg() to ask the user for the number of flips, and randi() to do the flips. Here's a start:
% Ask user for one integer number.
defaultValue = 5;
titleBar = 'Enter number of flips';
userPrompt = 'Enter flips';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Round to nearest integer in case they entered a floating point number.
numFlips = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(numFlips)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
numFlips = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', numFlips);
uiwait(warndlg(message));
end
% Now do the flips of the 3 coins.
numCoins = 3;
fprintf('1 = heads, 2 = tails\n');
for flip = 1 : numFlips
fprintf('\nHere is flip #%d\n', flip);
coinStates = randi(2, 1, numCoins)
end

Hristo Zhivomirov
Hristo Zhivomirov 2020 年 5 月 3 日
Hi Francesco,
My functions Flip a Coin or Roll a Dice with Matlab is exactly what you want. You can try them.
All best,
Hristo Zhivomirov

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by