They don't decide to "go" herm, and it most certaintly isn't genetics. One way is the silver colloidal treatment of plants to make a fem seed, doesn't always work and can sometimes grow a herm. The only other way is to stress a plant out o the point it has no other options for survival, but to quickly make male and female parts and at least get some seed before it dies. But, the most common way is for light to get onto the plant during it's dark hours. It is NOT when it germinates, is subjected to wrong temps (as long as they aren't over 100), or when fed during flower (I really hope you are feeding your plant during flower lol or you aren't gonna get anything).