Random number generation after time interval

18 Ansichten (letzte 30 Tage)
MeEngr
MeEngr am 14 Nov. 2014
Beantwortet: MeEngr am 14 Nov. 2014
I want to use the random value of two variables in a program. The program should keep running using these two values. After a certain time interval, the values should be updated with two new random values and theses should be used in the program. To summarize, the program should keep running and update the values of the variables after a time interval. Can someone help me?

Antworten (4)

Zack Bayhan
Zack Bayhan am 14 Nov. 2014
Bearbeitet: Zack Bayhan am 14 Nov. 2014
So if I read that right you want something like this code? Did you need just any random number? Or was there a particular type of distribution such as normal? Also I believe the rand function gives you a number from -1 to 1 so you may need to shape your random data scaling and transposing it.
tic;
clear all
t=1:10
for i=1:10
v1(i)=rand;
v2(i)=rand;
z(i)=4*v1(i)+5*v2(i)
end
plot(t,z)
toc;

Evan
Evan am 14 Nov. 2014
Bearbeitet: Evan am 14 Nov. 2014
I'm assuming this program involves a loop. If so, is each iteration quick enough that it's sufficient to simply check how much time has passed at each iteration and, if the limit is reached, reset the timer and the random number?
If so:
% Initialize the timer and random number.
tic
rN = rand;
for i = 1 : 60
% Check for random number reset
if toc > 5
rN = rand;
tic
end
% Some arbitrary operations here
pause(1)
% Display the current random number
disp(['rN = ' num2str(rN)])
end

Thomas Casey
Thomas Casey am 14 Nov. 2014
maybe try looping the operation you are performing with a pause(time) in the loop and a number of iterations that result in the desired time interval. put that in another loop that updates the variables each iteration and each time the inner loop finishes it'll grab the next variables and go at it again. its hard to tell exactly what you want without some more detail but maybe something along the lines of:
x=linspace(1,5,100);
for i=1:10
variables(1)=rand*10;
variables(2)=rand*5;
for j=1:5
y=(variables(1).*x)+variables(2);
plot(x,y,'o');xlim([1 5]);ylim([0 50]);
drawnow
pause(1)
end
end
This,for example, would draw a line with the first set of rand numbers for 5 X 1 seconds, then switch to new rand numbers and go for another 5 secs, etc., 10 times. Control your wait time on each set of variables using j and pause time.

MeEngr
MeEngr am 14 Nov. 2014
Thanks a lot, guys. I appreciate it :)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by