I love saying this: you're all wrong. (Except one person.)
The only way you get caught cheating is if you swap a card and the same card you swapped is already in play, be it in your hand, in another player's hand or on the cards placed face-up. This is why it is hard to cheat and why people usually get caught cheating at the end of the game.
For example, lets say the dealer places a Six of Hearts, and you just so happen to swap your first card with a Six of Hearts. Result: you're going to jail. If successful, however, you will receive the gold if you win the best hand. That's why I recommend swapping cards more to the end of the game, rather than to the beginning, when you're not sure if the dealer will place the card you just swapped.
Take it from the best cheater you'll ever play Poker with.
It has nothing to do with how many times you cheat, and it's not random. It's because of doubled cards, and the dealer knows there are never two of any cards. There's four Kings in total, but there's no two Kings of Hearts; only one.