I've created melodies in many different situations. One example: At home, I had my computer on a 70s internet radio station. I went to take a shower. The water is running, I couldn't recognize the song because I couldn't make out the melody. The only thing I could hear loud & clear were the bass & the beat. So I began to make up a melody based on these two predominant musical elements. When the song was done, I ran out of the shower, all wet and lucky I was the only one home, I raced to my 16 tracker & recorded the damn thing before I forgot it.
Personally, and when it doesn't come to me out of nowhere, in the middle of the night, after a dream, etc., I'm the kind of guy who likes to write a song/melody by experimenting with chords and progessions and singing to them. BTW, I sing falsetto.
And I love to experiment with 3 part harmonies. Listen to Crosby/Stills/Nash's "Carry On" from the album "Deja vu" and you get what I mean.