Random number generation after time interval
10 views (last 30 days)
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?
Zack Bayhan on 14 Nov 2014
Edited: Zack Bayhan on 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.
Evan on 14 Nov 2014
Edited: Evan on 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?
% Initialize the timer and random number.
rN = rand;
for i = 1 : 60
% Check for random number reset
if toc > 5
rN = rand;
% Some arbitrary operations here
% Display the current random number
disp(['rN = ' num2str(rN)])
Thomas Casey on 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:
plot(x,y,'o');xlim([1 5]);ylim([0 50]);
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.