Function that produce an uniform random numbers for loop error
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Faisal Al-Wazir
el 14 de Oct. de 2022
Comentada: Faisal Al-Wazir
el 14 de Oct. de 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 comentarios
Respuesta aceptada
KSSV
el 14 de Oct. de 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);
3 comentarios
Karen Yadira Lliguin León
el 14 de Oct. de 2022
maybe ?
Urand(i)= mod((la*seed*(i-1)),P);
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!