For most of the day, your penis is in a flaccid state (meaning it is soft). Erections can occur for any number of reasons, but are usually triggered by some sort of sexually arousing thoughts or stimulation. Young men typically begin getting erections during puberty as a normal part of their sexual development. Guys may also experience erections during their sleep, which can sometimes result in a “wet dream,” which occurs if one ejaculates while sleeping.
Weaker is your control over psychological activities, stronger is the link between your psychological activity with other internal activities without your own control where these internal activities also includes your biological activities and hence more are the chances that you're under your hormones's control. If you can control your psychological activities then its your wish that you want to link it to your internal biological activity every time you see a girl or only few times as per your control/wish !
The strength of this link between psychological activity and biological activity controls the amount of release of that hormone in the body. More is the release more restless your become! But once link between psychological and biological is established then it is difficult to revert back because by that time hormones are already released. Btw you can control them too but as I said its difficult.
It is common to young male so no need to worry. You have to learn the techniques so as to unnotice your erection by your friends and female friends.