Why won't this display?

2 vues (au cours des 30 derniers jours)
Jens Peter Juul
Jens Peter Juul le 6 Oct 2017
I am trying to display a random number x. I have tried the following for making the code display x but without success:
a = 16807;
b = 2147483647;
function [x] = lrng(m,a)
x(1)= (m-1)*rand(1)+1
for k=2:10
x(k)=mod(a*x(k-1),m);
end
fprintf('%.2f', x)
end
and
a = 16807;
b = 2147483647;
function [x] = lrng(m,a)
x(1)= (m-1)*rand(1)+1
for k=2:10
x(k)=mod(a*x(k-1),m);
end
disp(x)
end
Every time I try, I get the confirmation that the code has been run, but then it wont show me the output. What do I do? Have I made some mistakes regarding the code or is it something else?
Kind regards Jens

Réponse acceptée

Guillaume
Guillaume le 6 Oct 2017
Modifié(e) : Guillaume le 6 Oct 2017
Well, you're creating a variable a, a variable b, and a function lrgn but never do anything with them such as calling the function with your a and b variable.
So possibly, you meant to do:
a = ...
b = ...
lrng(b, a) %actually call the function!
function [x] = ...
Note that it's only in recent versions of matlab that you can define a function in a script. In earlier versions, your function would have had to be in a separate file, making more obvious that you never do anything with it.
  1 commentaire
Jens Peter Juul
Jens Peter Juul le 6 Oct 2017
Thanks for the answer :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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!

Translated by