No RNG involved. A unit is stunned when the amount of stun damage it sustained is greater than the amount of health it currently has (not maximum health, current health).
When you heal an enemy (or a soldier), you heal fatal wounds and usually give some amount of actual health as well. If the amount of stun damage was very close to the current health, increasing the health so it is now greater than stun means the unconscious unit will wake up. For this reason, in Piratez, it is better to heal stunned enemy with Vodka, which heals fatal wounds but does not restore any health, therefore doesn't help the enemy wake up. For soldiers, it is obviously better to restore as much health as possible, since that reduces the amount of time they will be unable to go on a mission.
Stun damage tends to drop over time, more so for some enemies than others in Piratez, which eventually will bring it under the amount of current health and they will wake up.