random selection of number among 2 numbers

8 views (last 30 days)
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.

Answers (1)

the cyclist
the cyclist on 9 Jun 2021
Edited: the cyclist on 9 Jun 2021
Here are a couple ways:
2*randi([0 1])
2*round(rand())
  2 Comments
Steven Lord
Steven Lord on 9 Jun 2021
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

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!