How to generate random numbers 0 and 1

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

Accepted Answer

Image Analyst
Image Analyst on 30 Jan 2015
Edited: Image Analyst on 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.

More Answers (2)

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

Sign in to comment.


Nawaf Alharbi
Nawaf Alharbi on 10 Sep 2018
r=randi(2,n,1)-1

Categories

Find more on Random Number Generation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!