Random delay / timer
robl last edited by
I'd like to do some occupancy simulation so that lights go on at slightly different times every day. I have a Time Program block, which is set for lights to come on at a certain time and/or when it gets dark at dusk, but I'd like to add a function to select a random number of seconds, or minutes between 0 and 60 minutes, and delay the ON from the timer (With a TON by some random time interval up to 60 minutes.)
I've played about with the RAND function and LIMIT, FLOOR, TRUNC etc but it's not quite what I need.
Say for example I want to pick a random number between 0 and 3600 (seconds), convert this to a time and pass it to TON. (Switch On Delay)
My function using rand can return a number higher than 3600, which LIMIT then just sets to 3600, so it returns 3600 more often and is not really random.
Any thoughts on how to do this? Seems like a useful function to have anyway. Could be a function that outputs a new number every time it receives an input, since RAND is a bit difficult to work with because it's constantly generating new random numbers, so it's hard to connect this to TON. (I had to use an Integer shift register + TP) to make it generate a new number just when it receives an input.
that can be achieved for example this way:
- The "max_random" input specifies the maximum number of seconds, which the random generator will output
- The "generate_random_time" is controlling input and the random time will be generated upon rising edge
- The "rand" block generates real number between 0-1.
- The "mul" block multiplies this number so you can get any number between 0 - "max_random"
- The "to_lint" converts the real value to lint
- The "B106 Integer Memory" can store the generated number and pass it to its output upon rising edge of the "generate_random_time" input
- The "mul_time_lint" has hidden input "in1" set to "t#1s". This time constant is multiplied by the generated number, thus generating random seconds.
Is this logic doing what you are looking for?
robl last edited by robl
Yes! So obvious when you see it! Thanks :)
Looking at the debug output, the rand block is running all the time, even though it is only needed to generate one number at a time. I am not sure if this wastes CPU, or if it is more efficient to only enable it when triggered, and turn it off again once the number has been generated. What do you think?
I took your logic and made it behave like TON but with a random time (so, the output of my timer will go in to this, then the output of this will be delayed by some random time, so the result would be, the light will come with a random delay of on up to an hour after 6PM, or whenever the time is set by the timer.)
(This first turns on the rand -> integer sequence for 1s, then after 1s, TON_0 switches on to capture number and to activate the actual switch on delay)