get two sum of two random number are not the same

2 ビュー (過去 30 日間)
Shuoze Xu
Shuoze Xu 2022 年 4 月 14 日
コメント済み: Shuoze Xu 2022 年 4 月 14 日
Hi.
I want to get diifferent nums of sum of two random numbers.
Which method should i use?
That is my code
% function file
function [card1,card2] = sendCard(c1,c2)
r = randi([1,10]); % fake random
c1 = 0; % initialize c1 and c2 as zero;
c2 = 0;
c1 = r + r;
c2 = r + r;
card1 = c1;
card2 = c2;
fprintf("The first user who have %d\n",card1);
fprintf("The second user who have %d\n",card1);
end
% driver file
[card1,card2] = sendCard(c1,c2);
% Expected output
The first user who have 10
The second user who have 15
% two sum must different
Thank you.

採用された回答

Davide Masiello
Davide Masiello 2022 年 4 月 14 日
編集済み: Davide Masiello 2022 年 4 月 14 日
If you need just 2 random integers as an output, you should do this
% Main file
sendCard
The first user has 11 The second user has 15
% Function file
function sendCard()
card1 = sum(randi(10,1,2));
card2 = sum(randi(10,1,2));
fprintf("The first user has %d\n",card1);
fprintf("The second user has %d\n",card2);
end
  5 件のコメント
Davide Masiello
Davide Masiello 2022 年 4 月 14 日
編集済み: Davide Masiello 2022 年 4 月 14 日
Sorry, I had understood that the two numbers needed to be different one form each other, since I used the while loop to make that sure.
I edited my answer based on your comment.
Now, a sum of two random integers (with max value of 10 for each integer) is assigned to each user.
Please let me know if this helps.
Shuoze Xu
Shuoze Xu 2022 年 4 月 14 日
I got it, thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by