How to generate random numbers 0 and 1

195 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2015-1-30
编辑:Image Analyst 2015-1-30
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 个)

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

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by