randomTrigger()
Interactive designers commonly want some events to happen occasionally, not on every step, in a random fashion. This function makes that easy: it gives you a simple yes/no (true/false) answer that, when checked in each step(), will trigger roughly once per second, minute, or any other time window that is specified.
You can think of it as a “random but controlled” switch: you decide how sparse or frequent the events should be (for example, about once every 5 seconds), and the function takes care of the rest, even if your loop runs very fast.
Example
#include <Plaquette.h>
DigitalOut led(13);
void begin() {
}
void step() {
// Returns true on average once every 5 seconds.
if (randomTrigger(5.0))
led.toggle(); // toggle LED
}
Reference
-
bool pq::randomTrigger(float timeWindow)
Randomly triggers an event about once per time window, on average based on sampling rate of primary engine.
Call this function once in each step(). It will occasionally return true, with the frequency adjusted so that you get roughly one event for each
timeWindow
period, no matter how fast your loop is running.- Parameters
timeWindow – duration of the window (in seconds)
- Returns
true when an event occurs during this sample
-
bool pq::randomTrigger(float timeWindow, float samplePeriod)
Randomly triggers an event about once per time window, on average.
Call this function once in each loop or sample. It will occasionally return true, with the frequency adjusted so that you get roughly one event for each
timeWindow
period, no matter how fast your loop is running.- Parameters
timeWindow – Duration of the window
samplePeriod – Duration of a sample (in same unit as timeWindow eg. seconds)
- Returns
true when an event occurs during this sample