Improvising melodies feels like a mess at the beginning. You don’t know how to hear direction. All the notes might be right, but they don’t sound like a line. A good melody, like a sentence, goes somewhere, has a rest, and either ends or continues. To illustrate this, go up a scale and rest on something other than the tonic. Hold it for a second and feel the inconclusiveness of it. Then go all the way to the top of the scale and back to the bottom. You feel why the first place you rested felt inconclusive.
Here’s a 15-minute exercise: create a small 3-4 note motif, not a big long string of notes. Pick a starting pitch, decide to move up or down by step, and then decide if you want the motif to sound closed or open. If it should feel closed, lead that last note back toward the key center. If not, bring it to a pitch that feels like a comma instead of a period. Do this a few times, alternating directions. You don’t need to create many, just notice the effect of the ending.
Also, don’t play every note with the same value or you will have melodic lines that sound like aimless wandering. In a good melody some notes are structural and some are just fill. So, spend a little more time on the notes that are in the chord and a little less on the passing tones. It doesn’t take much time to give the listener the impression of which notes are structural.
If you’re a beginning improviser, you may find yourself wondering how to come up with anything new, and that everything you come up with sounds like a nursery rhyme or a cliché. One solution is simply to take a song you know and just change the intervals. What you want to do is keep the general outline of the melody, so if it goes up three steps and then down two steps, maybe change it to going up two steps and down three steps. That way you keep some kind of logic to what you’re doing, but you’re changing what you’re doing. Again, it can be really helpful to record yourself doing this and listen back to yourself. Sometimes you’ll hear things that you didn’t realize you were doing while you were doing it.
After a while, you start to feel more and more like you know where you are going with the tones, that you can see where they’re going, that you know what will resolve and what will propel things forward. Keep playing around with two- or three-note phrases, and change their resolution to hear how that alters their effect. Eventually, you will get to the point where it doesn’t take much to hear a purpose in a couple of notes, to feel like they’re forming a line that says what you mean without having to be complicated.