random selection of number among 2 numbers

8 ビュー (過去 30 日間)
Akriti Raj
Akriti Raj 2021 年 6 月 9 日
コメント済み: Steven Lord 2021 年 6 月 9 日
How do I randomly choose among 0 and 2 in matlab ? I have display the choosen number also.
Note: The choosen number should be either 0 or 2 not something in between.

回答 (1 件)

the cyclist
the cyclist 2021 年 6 月 9 日
編集済み: the cyclist 2021 年 6 月 9 日
Here are a couple ways:
2*randi([0 1])
2*round(rand())
  2 件のコメント
Jan
Jan 2021 年 6 月 9 日
Or:
Pool = [0, 2];
x = Pool(randi([1, 2]))
Steven Lord
Steven Lord 2021 年 6 月 9 日
A generalized version of Jan's answer:
Pool = (0:5).^2;
desiredSize = [1 7];
x = Pool(randi(numel(Pool), desiredSize))
x = 1×7
25 16 4 0 25 9 1

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

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by