Reassigning array values in for loop
Mostra commenti meno recenti
I want to make a random number generator. It uses 3 formulas so I have 3 arrays which will store the values that are generated.
I currently have a for loop that generates the numbers but for some reason it is not updating the arrays. I tested the formulas by writing specific values for n and it does work so the problem seems to be in the loop.
Can anyone see why it is not working?
a1 = 0; a2 = 63308; a3 = -183326;
b1 = 86098; b2 = 0; b3 = -539608;
m1 = 2147483647; m2 = 2145483479;
%create an array with 10000 spaces
Z = zeros(1,10000);
X = zeros(1,10000);
Y = zeros(1,10000);
%To compute the random numbers, I need a seed for x and y
X(1) = 1; X(2) = 4; X(3) = 11;
Y(1) = 2; Y(2) = 9; Y(3) = 17;
x_n = @(n) mod((a1*X(n-1) + a2*X(n-2) + a3*X(n-3)),m1);
y_n = @(n) mod((b1*Y(n-1) + b2*Y(n-2) + b3*Y(n-3)),m2);
z_n = @(n) mod((x_n(n) - y_n(n)),m1);
for i=4:10000
X(i) = x_n(i);
Y(i) = y_n(i);
Z(i) = z_n(i);
end
disp(Z)
2 Commenti
dpb
il 28 Apr 2022
>> [X(1:10);Y(1:10);Z(1:10)]
ans =
1 4 11 69906 2.1474e+09 2.1455e+09 0 0 0 0
2 9 17 3.8445e+05 2.1406e+09 2.1363e+09 0 0 0 0
0 0 0 2.1472e+09 6.8197e+06 9.1569e+06 0 0 0 0
>>
Seems to do something for the variables you've calculated -- your loop only iterates over 4:6 so nothing other than those values can change.
Michelle Lear
il 28 Apr 2022
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!