How to generate random numbers 0 and 1

80 visualizaciones (últimos 30 días)
shruthi m
shruthi m el 30 de En. de 2015
Comentada: Kamal yahya el 27 de Feb. de 2020
I need to generate random numbers 0 and 1,if i use randi(1) it will be in fraction. exactly i need to generate 0 and 1 randomly???pls help

Respuesta aceptada

Image Analyst
Image Analyst el 30 de En. de 2015
Editada: Image Analyst el 30 de En. de 2015
Try
n = 10; % However many numbers you want.
randomNumbers = randi([0, 1], [1, n])
This will give a double. If you want integers, then cast to int32.
randomNumbers = int32(randi([0, 1], [1, n]));
or use logical() if you want booleans.

Más respuestas (2)

Michael Haderlein
Michael Haderlein el 30 de En. de 2015
e.g.
randi(2)-1
or
fix(rand*2)
Best regards,
Michael
  3 comentarios
Michael Haderlein
Michael Haderlein el 30 de En. de 2015
randi(2,n,1)-1
or
fix(rand(n,1)*2)
Kamal yahya
Kamal yahya el 27 de Feb. de 2020
grid=fix(randi([0 1],100));
imtool(grid)

Iniciar sesión para comentar.


Nawaf Alharbi
Nawaf Alharbi el 10 de Sept. de 2018
r=randi(2,n,1)-1

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