MZ Random variable between two values with boundaries

So I'm trying to spawn 4 (or any number) events close to another event that dies. I want them to appear randomly within (let's say) +/- 2 for both the X and Y of the original event. And the boundaries are that they have to be within the walkable part of the map, so a<(X +/- 2)<b and c<(Y +/- 2)<d. Is there some easy script for this?

I tried setting it up with event commands, but they just caused huge lag and didn't work, probably because of some loops inside of loops or something? I tried doing it so that I set a loop to spawn 4 events and inside of that was a loop where I tried achieving the coordinates within the boundaries with different control variables and conditionals. Can't figure it out at least at the moment.

EDIT: To be clear, the original event moves around, so its (X,Y) isn't fixed.
How are you spawning these events? If you're using a plugin for that, some of this functionality may be built in.

For example, VisuStella's Events and Movement Core lets you use JavaScript for the spawned event coordinates, and has optional checks for the area to be passable.
This is again the official ABS plugin I'm playing around with. To my knowledge it doesn't have that kind of functionality as Visustella events and movement core (and to my understanding it doesn't work with that plugin). I can only spawn the dynamic events to either fixed coordinates or variable coordinates. So I'd have to declare the variables elsewhere and place those in the plugin command.
Okay. I haven't looked at the ABS plugin at all, so I can't tell you how to get a point of reference for the event that's dying.

If you're running the script/event commands from within that event, you can use this

Otherwise you'd have to get the ID of the event and do something like $gameMap.events(ID-1)

I'll just use the word event and you'll replace it with an appropriate reference.

This will put X and Y coordinates into variables 5 and 10. Replace those numbers as you care.
Code:
let rand=Math.randomInt(5)+event.x-2;
$gameVariables.setValue(5, Math.max(0, Math.min($gameMap.width()-1, rand)));
rand=Math.randomInt(5)+event.y-2;
$gameVariables.setValue(10, Math.max(0, Math.min($gameMap.height()-1, rand)));

That will give X, Y coordinates that are +/- 2 from the original, and within the bounds of the map. You could use the Get Location Info event command to check for a terrain tag or region ID you want to use to indicate spots you don't want to spawn new events in.
Thanks a lot!
Must try and remember that method of first subtracting and then adding a random variable to create a range.

Seems to work fine. (The plugin doesn't want to play the death animation for the creatures I generated this way though...this is one messed up plugin, LOL)

Original thread: https://forums.rpgmakerweb.com/threads/random-variable-between-two-values-with-boundaries.158518/