Why am I having trouble getting an erection when with a girl in bed if I am only 18? How do I fix this? I still get full normal erections randomly.

Anxiety. Possibilities include performance anxiety and the wrong partner. It's reassuring that you are getting full normal erections randomly.
Anxiety. You are suffering from performance anxiety. Try to stay relaxed when you are with your girlfriend. Certain breathing/meditation/yoga techniques can help you with this problem.
See Urologist. This is almost certainly on an emotional basis since you occas get spontaneous erections. A brief visit can probably lead to improvement or complete cure.