Currently, I have a generator, but it doesn't sound very good. This is because it has no coherent anchor back. Here is another approach, that tries to follow an implicit chord progression:
Find the top N notes with the longest durations.
As it is picking figures randomly, when it is 3 or 4 notes out from one of the long ones, it can only pick from the figures that would land on that same note value. Being in a different octave is fine - in fact, that would help make this always feasible, as we are never more than 6 half-steps away from a suitable note.
Maybe we could have a control slider for the number (or proportion) of anchor notes.
As it stands, it works pretty well, but it still holds perhaps a bit too closely to the original melody.
Here is a concept to consider: