It hurts when I pee sometimes but not always, why?

Further tests. You could have a UTI if it hurts when you pee. Sometimes this could be a presentation of stds as well but further testing would need to be done. See your physician.