Self-consistent 3D radiative magnetohydrodynamic simulations of coronal rain formation and evolution