random selection of number among 2 numbers

8 visualizaciones (últimos 30 días)
Akriti Raj
Akriti Raj el 9 de Jun. de 2021
Comentada: Steven Lord el 9 de Jun. de 2021
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.

Respuestas (1)

the cyclist
the cyclist el 9 de Jun. de 2021
Editada: the cyclist el 9 de Jun. de 2021
Here are a couple ways:
2*randi([0 1])
2*round(rand())
  2 comentarios
Jan
Jan el 9 de Jun. de 2021
Or:
Pool = [0, 2];
x = Pool(randi([1, 2]))
Steven Lord
Steven Lord el 9 de Jun. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Random Number Generation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by