Function that produce an uniform random numbers for loop error
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Faisal Al-Wazir
am 14 Okt. 2022
Kommentiert: Faisal Al-Wazir
am 14 Okt. 2022
Hi i'm having an issue in my for loop
i',m trying to make a function that produce an uniform random numbers but no matter what i adjust i keep getting the same error
clear
clc
%N=input('sequence_length')
N=5000
MinSec = fix(clock);
seed = 100*MinSec(5) + MinSec(6);
Urand = myrandi(seed,N);
%b
%Y=ones(size(Urand));
subplot(2,1,1)
hist(Urand)
%% Task 1: : the Linear Congruential Generator (LCG)
v = rand(1,N);
%u=u./max(u);
subplot(3,2,2);
hist(v,100);
title(subplot(2,1,2),'Uniform random: rand()');
%Function that produce an uniform random numbers
%Using the technic of LCG r(k) = [lambda*r(k-1)]modulo(P)
function Urand = myrandi(seed,sequence_length)
la=65539
N=sequence_length;
P=2^31
%change N accordingly
%part a
Urand=zeros(1,N);
Urand(1)=seed;
for i=2:N
Urand(i)= mod(la*seed(i-1),P);
end
Urand=Urand/65539;
%printing genetrated vector, after dividing by 6655
if N<=50 %will print numbers only
if N<=50
disp(['for N = ',num2str(N)])
disp(Urand)
end
end
end%function myrand
0 Kommentare
Akzeptierte Antwort
KSSV
am 14 Okt. 2022
This line:
Urand(i)= mod(la*seed(i-1),P);
Your seed is a scalar, it has sinlge value. You are trying to extract more number by indexing it and treating it as a vector. This is the error.
Do you mean to use:
Urand(i)= mod(la*seed*(i-1),P);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Timing and presenting 2D and 3D stimuli finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!