Page 1 of 1

Using the random number function on C to make delays random

Posted: Fri Oct 28, 2011 2:33 pm
by ziggy1976
Hello, I would like to make interrupt or delays random in a program. So when a button is pressed, a buzzer is activated. A random delay or interrupt of 5 to 10 seconds should elapse, then the buzzer is activated again. A random delay between 2 and 10 sec' duration then starts. Is there a way to seed the random function, and then use the value generated to set the delay times?

Re: Using the random number function on C to make delays ran

Posted: Fri Oct 28, 2011 2:49 pm
by Benj
Hello Ziggy,

Dave S's posts on this topic should help.
http://www.matrixmultimedia.com/mmforum ... =29&t=8931

Re: Using the random number function on C to make delays ran

Posted: Fri Oct 28, 2011 6:47 pm
by ziggy1976
I've started the flow chart, I need help with the c-code. How do I use the random value to influence the delays?

Re: Using the random number function on C to make delays ran

Posted: Fri Oct 28, 2011 9:40 pm
by JonnyW
Hi Ziggy.

In your flowchart, create a variable and call it, say, 'rnd_seconds' - if you are only worried about an accuracy to the nearest second you can create this as a byte or int, it doesnt matter.

Now create a calculation icon containing the code:

Code: Select all

  rnd_seconds = random() % delay_time
Where delay_time is the time you would like to wait for as a maximum. You can then use rnd_seconds in the delay field and this will wait between 0 and delay_time.

I'm sorry, but I dont have v3, so cant post a solution file but the screenshot below shows a macro you could call that allows you to wait between 'min' and 'max' seconds. You can call this from the various places in your program. Note this macro uses local variables.

I hope this helps.
img.PNG
Macro to delay randomly
(21.18 KiB) Downloaded 2890 times
Jonny