If the headlight is flickering, then it sounds like the ballast needs replacement. If the headlight is on and then goes off randomly, sounds like it's the ground wire. Make sure the wire has a clear access to metal, and nothing obscuring it. I had the problem a couple weeks ago and I had to play with the ground wire to make the light come back on and so far, that has kept it from turning off.
Here is how my grounds are set up..
I don't know if this is how yours is, you'll notice that I had to scratch the surface in order to get a solid connection to the ground. Hope this helps man, I'll check back.