Why can't my jailbroken iPhone connect to a wifi network?
It may still be a hardware issue -- I had a similar problem last year. But since you say that your phone sometimes does see a wifi network, there's hope that the hardware is okay.

First, follow the comments posted here to make sure you're running a completely normal OS without any hacks or modifications.  

Then, here's Apple's description of how to reset the network settings.

Lastly, if it still won't work, check my link above.