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

 Réponse acceptée

Image Analyst
Image Analyst le 30 Jan 2015
Modifié(e) : Image Analyst le 30 Jan 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.

Plus de réponses (2)

e.g.
randi(2)-1
or
fix(rand*2)
Best regards,
Michael

3 commentaires

shruthi m
shruthi m le 30 Jan 2015
Thanku..if i need to generate random of n numbers of 0 and 1 ???
randi(2,n,1)-1
or
fix(rand(n,1)*2)
Kamal yahya
Kamal yahya le 27 Fév 2020
grid=fix(randi([0 1],100));
imtool(grid)

Connectez-vous pour commenter.

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by