This is something I've always been curious about. I don't think it's random. It seems to happen only when I make what I'd consider a fairly skillful shot. I'm just not sure how the game determines that, because it's kind of a subjective thing. But space sims have been doing it since at least the original X-Wing, so there must be some relatively simple mechanism for it.