There have been bugs reported with certain OSX Operating systems regarding your issue. There are a multitude of things you can do.
1. Update the OSX on your computer.
2. If the OSX is updated, try uninstalled that OSX and re-installing it. Sometimes there are issues with typical functions when the computer is being installed or updated.
3. Try switching accounts, often times there are some circumstances that have altered the settings on your computer. In such a case, create a new account and test if it is doing the same thing.
4. If none of these solutions have helped, your computer may have a manufactures default and I suggest you take it to your nearest Apple Store or Certified apple care specialist.
I hope this could be of help.
If this helped you, please accept the answer so that it can help the community find this answer if they are having the same issue as you.