If your phone is randomly charging it probably has to do with either the power cord and the micro-usb connection. Try using a different cord to see if that fixes the issue, if not try wiggling the cord to see if that generates a response from the power cord. If none of those work look up the troubleshooting guide on how to replace the micro-usb connector as that is the likely problem.