How to generate random numbers 0 and 1

78 ビュー (過去 30 日間)
shruthi m
shruthi m 2015 年 1 月 30 日
コメント済み: Kamal yahya 2020 年 2 月 27 日
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.
  1 件のコメント
shruthi m
shruthi m 2015 年 1 月 30 日
thanku..

サインインしてコメントする。

その他の回答 (2 件)

Michael Haderlein
Michael Haderlein 2015 年 1 月 30 日
e.g.
randi(2)-1
or
fix(rand*2)
Best regards,
Michael
  3 件のコメント
Michael Haderlein
Michael Haderlein 2015 年 1 月 30 日
randi(2,n,1)-1
or
fix(rand(n,1)*2)
Kamal yahya
Kamal yahya 2020 年 2 月 27 日
grid=fix(randi([0 1],100));
imtool(grid)

サインインしてコメントする。


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

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by