Generating random numbers using for loop

34 ビュー (過去 30 日間)
Maheen Fazal
Maheen Fazal 2019 年 2 月 7 日
回答済み: Image Analyst 2022 年 6 月 18 日
Hi, Shall i use the following code to generate random numbers and use them further in the calculation?
clear all
close all
for n=1:2;
h(n)=rand
end
  1 件のコメント
Praveen
Praveen 2022 年 6 月 18 日
rand(0,1)

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

採用された回答

Walter Roberson
Walter Roberson 2019 年 2 月 7 日
If you like. you could instead pass size information to rand to say how many items to generate .
  1 件のコメント
Maheen Fazal
Maheen Fazal 2019 年 2 月 7 日
Sir how?

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

その他の回答 (2 件)

KSSV
KSSV 2019 年 2 月 7 日
編集済み: KSSV 2019 年 2 月 7 日
N = 10 ;
iwant = zeros(N,1) ;
for n=1:2;
iwant(n)=rand ;
end
iwant
OR
N = 10 ;
iwant = rand(N,1) ;
Read about rand
  1 件のコメント
Maheen Fazal
Maheen Fazal 2019 年 2 月 7 日
Thank you so much

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


Image Analyst
Image Analyst 2022 年 6 月 18 日
@Maheen Fazal did you look at the help? If you would, you'd see info like this:
Random Numbers Within Specified Interval
Generate a 10-by-1 column vector of uniformly distributed numbers in the interval (-5,5).
r = -5 + (5+5)*rand(10,1)
r = 10×1
0.3467 -2.7506 0.3305 -0.3516 0.4854 4.9774 -4.3315 -4.5855 -3.7174 1.3212
So, for you:
h = rand(1, 2)
h = 1×2
0.1069 0.1859

カテゴリ

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