Dictionary.com
Thesaurus.com

method in one's madness

Idioms  
  1. An underlying purpose in crazy behavior, as in Harry takes seemingly random trips around the country but there's method to his madness—he's checking on real estate values. This expression comes from Shakespeare's Hamlet (2:2): “Though this be madness, yet there is method in it.” For a modern equivalent, see crazy like a fox.