Why a safety car changes the maths
Under a safety car, the whole field slows down, which shrinks the time lost by any car that pits during the period, since rivals are also driving slowly rather than at full pace. A stop that would normally cost significant time relative to the field can become almost free, which is why teams with a car still due to stop will often do so immediately once a safety car is deployed.
Who benefits and who does not
This effect does not touch every car equally: a driver who has already made their stop gains nothing from the safety car and may even lose position to rivals who stop cheaply under it, while a driver who was planning to stop soon anyway benefits enormously from the timing. The unevenness is what produces the sense of luck, since two drivers can make identical strategic decisions and see them turn out completely differently purely because of when the safety car appeared.
Distinguishing luck from structure
Not all of this is random in the way it feels: teams that delay a marginal stop are consciously gambling on a safety car appearing, aware that certain circuits and race situations make one more likely. What looks like a lucky break is sometimes a deliberate bet paying off, and separating the two requires knowing what a team's plan was before the safety car appeared, not just what happened afterwards.

