why are prime numbers beter when writing codes?

If you are writing pseudo-random numbers, they repeat less often because primes have no common factors.

"writing codes" has many meanings, but breaking codes usually involves factoring large numbers. Do a web search on primes and encryption, and you will find a wealth of articles.