I need to make a program that generates random numbers between 0 and 9999 for 5 seconds, and stops if the number 2015 is generated. I must use the etime and fix functions.

this is what I've done so far but it doesn't seem to be working... Could anyone help me on this?
t1=clock;
while 1
t2=clock;
if etime(t2,t1)>=5.0;
Disp('not found');
break;
end
x=9999*rand(1);
Y=fix(x);
Disp(Y);
if Y == 2015
Disp('found');
break;
end
end

 Respuesta aceptada

After I've replaced Disp with disp the program works fine. Another way to solve the task would be the following program; it uses variables instead of the 'magic numbers' 9999, 5, 2015, a single line to generate the random number, and puts the condition where the while loop is left in the while condition, instead of using while 1 and break. Also only a single if with and else condition is used, instead of two ifs.
Ystop = 2015; % define a value where the loop should stop
Y = Ystop - 1; % some value different from stop value such that the while
% loop is entered
Tstop = 5.0; % define a time where the loop should stop
Maxrand = 9999;
t1 = clock;
while Y ~= Ystop && etime(clock,t1) < Tstop
Y = fix(Maxrand*rand);
disp(Y)
end
if Y == Ystop
disp('found')
disp(['after ' num2str(etime(clock,t1)) ' seconds'])
else
disp('not found')
end

Más respuestas (1)

You know that iso-9001 does not care about which functions you call, only that you document your process completely. And the British "Redbook" and "Bluebook" security standards said nothing at all about MATLAB (and are completely replaced anyhow.) If your jurisdiction outlawed some MATLAB calls then you are going to need to point us to the source text of the law so we do not inadvertently break the rules.
Or wait a second... Is this perhaps homework that has not been marked as such??

2 comentarios

Im sorry but I honestly have no idea what you are talking about.. This is simply a school assignment that I need some help on. We only learned about matlab very briefly in our class and neither my classmates or I know how to solve this problem. I feel like the reason what I did is not working is that I put both t1 and t2 are equal to 'clock' but I'm not sure...
What I am talking about is that we expect students to specifically indicate that they are working on homework / assignments. Assignments sometimes impose restrictions that are completely unrealistic in real life.
When you ask Questions and there are restrictions on the acceptable solutions, you should indicate the restrictions and the reason for them, whether the reason is "assignment", "company policy", "legal requirements", "must meet a particular standard", "economic", "low power solution needed" and so on. Otherwise people end up wasting their time preparing explanations or solutions that are not relevant to the situation.

Iniciar sesión para comentar.

Categorías

Productos

Preguntada:

el 18 de Nov. de 2015

Comentada:

el 19 de Nov. de 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by