Random number generation after time interval
10 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(4 个)
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;
0 个评论
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
0 个评论
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!