The most common reason your light only works sometimes is because of a loose wire connection. If there is a slight interruption in the electrical current, lights will work sporadically, regardless if the switch is on or off.  You may also be facing a problem with the light fixture itself. For example, if the bulb is not secure in its housing, the connection might be sporadic and only work sometimes.