Answer:
Hamlet's most mad-seeming outburst, against Ophelia, may be explained by the fact that Claudius and Polonius are spying on the conversation: if Hamlet suspects that he's being spied on, he may be acting more deranged than he really is for the benefit of his listeners.