MATLAB Answers

Generating random numbers using for loop

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

Accepted Answer

Walter Roberson
Walter Roberson on 7 Feb 2019
If you like. you could instead pass size information to rand to say how many items to generate .

More Answers (1)

KSSV
KSSV on 7 Feb 2019
Edited: KSSV on 7 Feb 2019
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

Community Treasure Hunt

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

Start Hunting!