random selection of number among 2 numbers

조회 수: 10 (최근 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 CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by