Trigger function randomly with mousePressed

Hello,

I would like to trigger a function or event randomly within an unknown sequence of mousePressed (since it is unpredictable how many times will the user press the mouse).
May I ask if it is feasible please? If so, it would be very nice if anyone could give me any suggestions :)))

Cheers,
Karen

If I understood your question, there are many ways to do so. Solely rely on randomness, can be “problematic”. For exemple, if you do:

void ofApp::mousePressed(int x, int y, int button){
    
    if (ofRandomuf() < 0.1f) {
        // your event/function
    }

}

You are giving the user 10% change for the event to happen. (0.5f for 50%, 0.75f for 75% and so on).

But, this also means that you can press the mouse a thousand times and nothing happens… Therefor, it’s better to have more control of how many inputs the user did, like so:

int numberOfInputs = 0, limitInputs = ofRandom(15, 30);

void ofApp::mousePressed(int x, int y, int button){
    
    numberOfInputs++;
    
    if ((ofRandomuf() < 0.1f) || numberOfInputs >= limitInputs) {
        
        // your event/function
        
        numberOfInputs = 0;
        limitInputs = ofRandom(15, 30);
    
    }

}

By doing this, you guarantee that no matter what, after between 15 to 30 inputs, the event will trigger. The user will still have a feeling of randomness, but you have a ceiling.

There are many other ways to approach this, but I hope this is useful to you.

1 Like

THANKS SOOO MUCH for your detailed explanation :))) x