Function that produce an uniform random numbers for loop error
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Faisal Al-Wazir
le 14 Oct 2022
Commenté : Faisal Al-Wazir
le 14 Oct 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 commentaires
Réponse acceptée
KSSV
le 14 Oct 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);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Timing and presenting 2D and 3D stimuli dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!