It's not uncommon for cats to suddenly bite while being petted. This is called "overstimulation" and is most often caused by stress or built up frustration. This is created from either the lack of interaction or from over exposure to a stimulus - and often an unattainable one, like a bird at the window.
Cats will have a variety of reactions to overstimulation and you should be aware of what to watch for - cease petting and stop interaction before a bite happens. Be aware of:
• Your cat's tail twitching
• Cat's ears turning back or flicking back and forth
• The cat's skin rippling when you pet him