Just thought I'd share this very effective way of coming up with music in my head:
Meditate until you've built up enough concentration that it lasts for a half-hour or so after you've finished.
Close off all sources of light into a room, then lie down somewhere comfortable (I do this at night lying in bed).
Stick earplugs in your ears to cut off background noise (very important I find even if there are only faint sounds of traffic).
Spend about 10 minutes thinking up as much original music as possible of any kind until you become relaxed, unselfconscious and generally in a state of creative 'flow'.
Then start directing your imagination towards the ideas that you want.
I think this is so effective (for me) because it kind of works like a sensory-deprivation tank. I realise that many people don't find in-head composing effective at all, but for those who do, you should definitely give this a try. I find I can come-up with far more original melodies than when I'm improvising, and the better I get at meditation, the more effective this technique becomes. I just hope this all doesn't sound too weird in an off-putting way.