Random number generation after time interval

조회 수: 7 (최근 30일)
MeEngr
MeEngr 2014년 11월 14일
답변: MeEngr 2014년 11월 14일
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?

답변 (4개)

Zack Bayhan
Zack Bayhan 2014년 11월 14일
편집: Zack Bayhan 2014년 11월 14일
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 2014년 11월 14일
편집: Evan 2014년 11월 14일
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 2014년 11월 14일
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 2014년 11월 14일
Thanks a lot, guys. I appreciate it :)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by