It is certainly understandable for someone who has just lost his father, and gained a stepfather to suddenly go mad. However, some time passes before Hamlet is "mad". In fact, before he even begins showing signs of madness, he says to his friend Horatio "As I perchance hereafter shall think meet to put an antic disposition on" (Act I, scene V, lines 189-190). It is not until after this statement that Hamlet becomes mad. The purpose of this statement is to tell Horatio not to worry if Hamlet appears strange because he will be acting mad, in order to get attention from King Claudius. Later, as Hamlet is speaking to Guildenstern, he makes the analogy that he is "but mad north-north-west: when the wind is southerly I know a hawk from a handsaw" (Act II, scene II, lines 397-398), again indicating that he is only shamming insanity. Also, in a heated conversation in which his mother is questioning his sanity, Hamlet says "I essentially am not in madness, But mad in craft" (Act III, scene IV, lines 206-207). Hamlet is putting on an act, a deceiving performance in order to confirm who was involved with his father's death.
However, Hamlet only performs his act for certain characters. Only in the presence of Gertrude, Claudius, Ophelia, Polonius, Rosencrantz, and Guildenstern does he behave as a madman. These are the characters whom Hamlet may have reason to suspect of a part in his father's death. By feigning madness, Hamlet confuses these characters, in hope of learning the truth of the murder of the king. In the third Act, Hamlet is set up to confront Ophelia, and promptly displays an antic disposition. He speaks in circles and contradicts himself plainly telling her "I did love you once" (Act III, scene I, line 124), and then in his next dialogue "I loved you not" (Act III, scene I, line 128). However, in different company, like that of Horatio, Bernardo, Francisco, the players, and the gravediggers, he is perfectly sensible and he displays his skills of witty and intellectual conversation. Horatio doesn’t comment on whether he thinks Hamlet feigning madness is a good idea or not; he just tells Hamlet to be careful.
It is unclear whether Hamlet wanted Polonius to see the “method behind his madness”. Even though Hamlet’s performance as a madman is a good one, Polonius notes that "Though this be madness, yet there is method in it" (Act II, scene II, lines 220-221), hinting that he is catching on to Hamlet's plan to feign his madness. Polonius sees a reason behind the insanity, proving that Hamlet is in fact acting as a madman. King Claudius is also suspicious of the reason for Hamlet's madness. After witnessing the meeting between Hamlet and Ophelia, he makes the decision to keep an eye on Hamlet, saying "Madness in great ones must not unwatch'd go" (Act III, scene II, line 201). He is not totally convinced of Hamlet’s insanity, even though queen Gertrude is trying to convince him that the reason behind Hamlet’s madness is the unexpected death of his father. Claudius realizes that this madness that Hamlet is showing could be dangerous to himself, or his kingdom.
Though Shakespeare never says what the method is behind Hamlet's madness, it is apparent that he is indeed feigning his insanity. He was visited by his father's ghost, which tells him that he had been murdered by Hamlet's uncle, Claudius, and that he must take revenge. While Hamlet knows that his father was wronged, he must decide whether or not to avenge his death. Acting quickly, Hamlet assumes a role of insanity in an effort to confirm his suspicions about Claudius' involvement. He admits to other various characters that he is putting on an act, and only plays this act for certain characters, a couple of whom sense that there is a reason behind it. He is cruel to these characters that he can't trust, but as Hamlet himself says "I must be cruel, only to be kind: Thus bad begins and worse remains behind" (Act III, scene IV, lines 196-197).