Most desktops shut down like that when the power supply is going bad. One time in my computer repair days I seen a bad connector from the power supply to the motherboard. Other things that can cause it to shut down is if any of the fans are not working. Air flow is real important. You have to open up the case and make sure the fan on the CPU chip is spinning, as well any case fans. Some of the cheaper computers do not have case fans and rely on the fan built into the power supply which brings us back to a faulty power supply.

Now for laptops, in my experience, if it is shutting down already the damage is done. You can try to make sure their is no dirt or dust build up around the fan on the case, but other than that I wouldn't recommend opening up a laptop unless you know about static safety.

You could also have your "power options" set to put it to sleep after a certain period of inactivity, and quite often it goes into "coma" mode rather than "sleep" and has to be restarted. Fans can also fail, or get blocked with dust, and overheat the CPU which will also shut it down. Dust on the mother board and in the CPU's heat sink will cause the same problem.

Late in the game and probably won't be read, but you may have a much simpler thing going on. If you get Windows Updates automatically via the web, you could have it set to reboot when the install is complete. That's the default I think. Many, but not all, updates require a reboot. Another possibility is a very quick loss of power in the house. The power on my side of town is unreliable for a big city, and we get drops even on sunny days. I'll wake up and find half the electric clocks in the house blinking, or my PC rebooted.