Everything has a logic, albeit one that is not clear to the outside world. There is even an internal logic to most madness, a belief system, perhaps, that leads the mad person from Point A to Point B with astounding logic, whether that is Daisies = Mother’s Meat Hook = The Florist is trying to Kill Me. We don’t see all the steps and associations in between but the mind is inventing its own rules.
Liars are said to have more neural pathways in the brain, allowing them to more speedily invent another reality. And despite the fact that madness seems random sometimes – it doesn’t often widen the possibilities of the mad, it tends to narrow them. Movement vocabulary tends to diminish, a compulsive gesture can be repeated and repeated. A socially inappropriate phrase bubbles up on repeat rotation.
There is always some kind of method, whether it is conscious or no.
Hamlet’s madness is, of course, quite conscious in that it is not madness at all. Polonius isn’t stupid, he suspects something’s up. He’s dealing with a confirmation bias, though, so he cannot dwell on those suspicions.