First, be aware that you should never use a second-hand machine as-is. You don't know what state the previous owner left it in. There could be malware on it, perhaps even intentionally placed there by the previous owner. There could be all manner of crap software installed. And the system's state is unknown - it could be damaged somehow.
What you need to do is erase the hard drive, then reinstall whatever system it originally came with. Although written for the seller's perspective, see How to prepare your Mac for sale for information on how to do that. Do not try to restore backups of anything on the machine... unless the licenses were transferred legally to you, none of the third-party apps are yours, and cannot be legally used by you. Besides which, it's often not easily possible to restore apps from a backup. If the licenses were transferred, you can reinstall those apps from scratch once you have a clean system in place.
This may very well solve your problem. If it doesn't, it's likely to be a hardware problem of some kind. For more information on possible causes, see:
My mouse keeps moving around on its own, as if someone is remotely controlling my Mac!
Finally, note that you should never trust what a seller says about a computer they're selling. They may tell you there's nothing wrong with it, but before you hand over your cash, you should verify that. Not turning the machine on for three months after purchasing it gives a dishonest seller plenty of time to hide his/her tracks.
(Fair disclosure: The Safe Mac is my site, and contains a Donate button, so I may receive compensation for providing links to The Safe Mac. Donations are not required.)