How to generate random numbers 0 and 1

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

 采纳的回答

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.

更多回答(2 个)

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

3 个评论

Thanku..if i need to generate random of n numbers of 0 and 1 ???
grid=fix(randi([0 1],100));
imtool(grid)

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by