I would get an AC line tester to verify the outlets wiring. I would start there
You likely need to get an electrician to fix your house wiring as you do need a good grounded outlet. This sounds like a classic case of cross-wiring.
Lets look at this from an electrical wiring perspective we have a source (+) and a drain (-) which is also tied to the buildings ground. If the device is expecting a given blade (pin) within the power cord to be the + side then you plug it into the - side then you've cross-wired the device!
As the iMac case is grounded you now have a live case (+) so when you touch it and have your foot on a wet floor which is tied to ground (-) you'll get zapped! This is a case of bad power wiring.
So why does other devices not encounter this problem? Not all devices have their cases tied to the negative side of the cord (-) we call these cases double insulated as they have no exposed metal surfaces that are electrically active. A hair drier is a good example.
Even still it's the way you plugged the plug in that also makes a difference! Again, your iMac presently is cross-wired. If you flip the cord plug 180 degrees that should resolve it. But, you might not be able to!
Lets say you have a three prong grounded plug and the outlet is also designed to support the three prongs of the plug (but the ground connection is not connected as you stated). Then we can surmise the outlet is mis-wired! Here the brass screw has the White wire (common) when it should have the Black wire (hot). Here we can't rotate the plug 180 degrees to move the expected + of the plug to the + side of the outlet.
So is the fault the computer? No! It's your houses wiring that needs to be fixed. Make sure you get a proper outlet installed and make sure it's wired correctly!
As to why the problem comes and goes its possible you have something that is sorted across to ground so the ground is hot! A motor or heater is often the cause.