What would be the best approach to firing an event randomly each step?

Here's the story: The character is in a dusty shed. Sadly, he's allergic to dust. So, I'd like to give him, say, 20% chance that he sneezes each time he makes a step.* This means that if the rng decides he's gonna sneeze then the movement is interrupted and a dialogue with a sneeze appears; but, if the rng decides he's not gonna sneeze yet then the movement must not be interrupted.

What is the best approach to do this?

One idea to do this sans javascript is to place events with priority below characters ALL OVER THE PLACE, assign each a switch, and give each two pages: when its switch is on then its activated on player touch, rerandomizes switches, and shows dialogue. One problem with this approach is that this will mean that if two adjacent tiles have their corresponding switches off then the player ma move his character back and forth infinitely without the sneeze showing up. The solution would be to rerandomize switches even on non-sneezing tiles, but this would require setting all tiles to activation on player touch and this would break auto movement.

Or is there any plugin that would do something like this around alredy? Or if there is none, could you give me a hint how should I tackle this? Note I'm not asking you to write it for me, just for a hint.

* Yes, I know this would be annoying to players. But well, that's the point.
I think maybe have each step trigger a common event that runs a script to do a random number roll 1-100 and assigns that value to a variable.
Then have a simple conditional - if variable X is between 1-20, sneeze, else nothing.
I think maybe have each step trigger a common event that runs a script to do a random number roll 1-100 and assigns that value to a variable.
Then have a simple conditional - if variable X is between 1-20, sneeze, else nothing.

I believe that calling any events while moving breaks auto movement with mouse clicks. That is, if any event is called while the player character autonavigates to the clicked destination, then the player character stops. I'd like to check if I'm right, but sadly I'm not sure what you mean by "have each step trigger a common event" - should I make each steppable tile an event that is activated on player step that calls this common event? In that case I'm sure this _will_ break mouse movement.

Anyway, this is how I seem to have achieved this:

Code:
var _Game_Map_setup = Game_Map.prototype.setup
Game_Map.prototype.setup = function(mapId) {
   _Game_Map_setup.apply(this, arguments)
   var _Game_Actor_onPlayerWalk = Game_Actor.prototype.onPlayerWalk
   if(mapId == 1) { // dusty shed
       Game_Actor.prototype.onPlayerWalk = function() {
           var ret = _Game_Actor_onPlayerWalk.apply(this, arguments)
           if(Math.random() >= 0.8)
               $gameSwitches.setValue(13, true) // P1Sneeze - runs Sneezer common event
           return ret
       }
   } else Game_Actor.prototype.onPlayerWalk = _Game_Actor_onPlayerWalk
}



Comments? Anything wrong here?
I know this is a few months old and necroing is bad, but I found this code really helpful by doing a forum search, to solve some issues of mine about detecting player movement (I had an ugly but working method, this one is way cleaner).

Unfortunately @kmph did some mistakes in his original code, I share a "what I believe to be" cleaner version:

Code:
// Hook on Player Walk
var _Game_Map_setup = Game_Map.prototype.setup;
Game_Map.prototype.setup = function(mapId) {
    _Game_Map_setup.call(this, mapId);
    // Eventually control mapId here to branch on different ways to handle the hook
    var _Game_Actor_onPlayerWalk = Game_Actor.prototype.onPlayerWalk;
    Game_Actor.prototype.onPlayerWalk = function() {
        _Game_Actor_onPlayerWalk.call(this);
        // DO YOUR STUFF HERE
    };
};

Hope it helps!

Original thread: https://forums.rpgmakerweb.com/threads/what-would-be-the-best-approach-to-firing-an-event-randomly-each-step.86321/