method to one's madness
Do things in an unorthodox fashion, yet nevertheless achieve the intended result. Yet again Shakespeare's Hamlet provided a phrase that was picked up and used through the ages. Having observed Hamlet rave on in what appeared to be senseless sentences, Polonious makes a comment that turns out to be true: “Though this be madness, yet there is method in't.” Under less dramatic circumstances, the phrase applies to getting the right outcome by what seems to be the wrong method, and we've all done that.