Our (and Luke's) original impression of Yoda is by no means flattering. He looks like a crazy hungry green thing that unfortunately has the ability to speak. Yoda digs through Luke's stuff, eats Luke's food, and, obsessed with that awesome little flashlight, ends up stealing it. (He even goes so far as to fight with Artoo over it.)
So was Yoda slowly losing it during all those years as a hermit?
Perhaps going from fighting battles and conversing with others on a daily basis to living in total isolation with no one to speak with would play a part in that? Or could Yoda have been faking his apparent insanity for some reason? (If so, what is mentioned reason?)
Pretending to be the nutty old harmless thing so Luke would not know who he was.
See Obfuscating Stupidity.
The reasons were:
He didn't want to teach Luke (being wary of another Skywalker Reversal) due to Luke having similar problems to Anakin (started too late, wrong personality traits, Skywalker Curse, and excessive Midichloran count meaning that the downside of Luke falling to Dark Side was that much higher).
He didn't know if he could risk the info that he was Yoda the Jedi Master to fall into Luke's hands yet.
Luke not knowing who he was helped him test Luke by observing Luke in "natural" state (e.g. NOT the state of Luke talking to great Jedi Master).
In addition, that behavior was testing Luke (e.g. would he react with violence to stealing of flashlight or food? How would he react to having his patience tested by seeing this nutcase when he's in a rush to find Jedi Master?).
This is not just a logical speculation, but is confirmed in C-Canon:
Yoda had also given Luke a hard time at that encounter. It had been a test of Luke's patience and of his treatment of strangers. And Luke had flunked it. Rather miserably. (Dark Force Rising, Timothy Zahn)
Also, as far as behaving in weird manner, it's a standard archetype/trope for a Quirky Weird Wise Wizard Mentor. Somewhat covered by Old Master but I'm too afraid for tvtropes to go dig for more specific trope names.
Yoda was purposefully being as annoying as he possibly could, to test Luke's patience. Patience is key in all aspects of the "Light Side" of the Force; Jedi trained as Younglings and/or Padawans for years, even decades, to become a Jedi Knight. The Dark Path is easy and fast, and those are powerful lures. Yoda needed to be sure that Luke wouldn't follow in his father's footsteps.
Luke WAS impatient, and he lost his hand because of it, but during Luke's training Yoda also took extra care to hammer into him what the consequences of starting down the Dark Path would be, and so Luke was able to make the right decisions despite his rather impulsive moves to place himself in temptation's path.
It seemed to me he was doing two things, having fun playing the fool, and evaluating Luke's demeanor. Yoda considered Anakin's betrayal as a personal failing for his failure to foresee and prevent the destruction of the order. Taking on the training of another Skywalker would not have been done lightly.
As for being alone you are never alone when you have the force. While that may sound corny remember that through the force Yoda could connect with other living creatures on a level we do not understand. Not to mention communicating with Quijon and Obi Wan.
Has anyone taken into account the loss of several hundreds of Jedi? In ROTS, when Mace and a few other Jedi die he falls to the ground holding his head. With the other hundreds then dying soon after and over the coming years it's possible it was psychologically traumatic for Yoda. Hence his supposed 'insanity' in the Original Trilogy.
I think that Yoda had to be testing Luke for multiple reasons. For one, and this one has already been stated, Yoda keeps telling Luke over and over that he must be patient. Another reason is that Yoda mostly drops the act when Luke figures out who he is. My final reason, but not the last reason I have, is that Yoda is heard talking to Obi-Wan, saying that Luke was not ready. Besides, was Yoda ever completely sane. He was always wise, yes, but completely sane, no.
Yoda was being annoying and faking being crazy because he was testing Luke's patience to see if Luke is ready for Jedi training. If you remember the scene where Yoda and Luke are in Yoda's hut, Yoda started to talk to Obi-wan and said that Luke is not ready for training.
Well, Yoda is (of course) a figment of the imagination. This is the first time he appears in this collection of stories and I believe that this was not some "act" on this characters part to dupe or test Luke. This is the way that Yoda was meant to be portrayed and was only later refined into some great leader of the Jedi to fill his role in earlier episodes. It is a major flaw that we Star Wars fans strive to correct by inventing these multiple theories. These theories that we concoct are wonderfully creative and I will gladly read and ponder them but, let us be earnest here. The character of Yoda, if a REAL living being, would be considered to be at best severely bipolar, and most likely he would be psychotic/and or in the grip of a multiple personality disorder. Please be kind to me in your replies. I LOVE SW...even the last 3 films and the cartoon(s). My wife makes fun of me and my buddies when we nerd out on SW. I do truly believe that when created Yoda was to be a much more hermit like individual with NO social graces. I don't think Lucas foresaw him EVER being a leading member of the Jedi High Council or schmoozing with dignitaries as he negotiated policy for millions upon millions of sentient beings. This high council Yoda would NEVER rifle through someones belongings and consume their food/steal trinkets and then have fisticuffs with a droid over it. NOT logical for someone that is SO in tune with the force. So, with that(and many other unlisted points) in mind I stand with my aforementioned premise that it is simply poor character development or character planning. Maybe he should have had Yoda suffer some type of brain damage that was not properly diagnosed or treated.....this I could believe would cause him to act in the manner he does when he meets Luke in the swamp. Damn, that is a pretty good idea! Just thought of that as I typed. Maybe some type of viral infection/mechanical injury that damaged his brain or neural chemistry could be worked into story. Sounds like a great fan fiction short story.