If your computer is randomly going to sleep, the problem might simply be due to a low value being selected for the Sleep Time Settings on the computer.
If this is not the case, the problem could be to due to outdated drivers, computer switching to a Blank screen saver or Hybrid sleep, which has been associated with sleep related problems.
Windows Computer Goes to Sleep Randomly
In most cases, the problem of a Windows computer randomly going to sleep ends up being due to misconfigured sleep mode settings, which can be easily fixed by selecting the recommended sleep mode settings.
If this does not help, you should be able to fix the problem by using other troubleshooting steps as provided below in this guide.
1. Check Power & Sleep Settings
To start with, take a look at “Power & Battery” settings on your computer and make sure that your computer is set to go to sleep after a reasonable period of inactivity.
1. Go to Settings > System > Power & Battery.
2. On the next screen, expand Screen, Sleep & Hibernate time-outs tab > and select your desired Sleep time for “Make my Device sleep after” entry, both for Battery & Plugged-in sections.

Note: It is better to go by the recommended sleep settings for Battery. For Plugged-in, you can select your desired or longer sleep time (As required).
2. Change What Power Buttons Do
Windows provide the option to associate the power button with sleep mode. Since, your computer is going through sleep related problems, it is best not to associate the power button with any action.
1. Open the Control Panel on your computer.
2. On Control Panel screen, switch to Large Icons view and click on Power Options.

3. On Power Options screen, click on Choose what the power buttons do link.

4. On the next screen, select the Do Nothing option for all the entries.

5. Make sure that you click on Save Changes button.
3. Check Screen Saver Settings
It is possible that you may have accidentally or mistakenly selected the Blank Screen Saver for the Lock Screen on your computer.
If the Blank Screen Saver is set to activate after 5 minutes of inactivity, it can make the computer look like it has either been switched OFF or is in Sleep Mode.
1. Go to Settings > Personalization > Lock Screen.
2. On the next screen, scroll down to “Related settings” section and click on Screen Saver.

3. On the Screen Saver Settings screen, remove the Blank Screen Saver and select the None option.

4. Click on Apply and OK to save this change on your computer.
4. Update Graphics Driver
The problem could be due to the Graphics becoming outdated and incompatible with the operating system or an App/program on the computer.
1. Right-click on the Taskbar and click on Device Manager.
2. On the next screen, expand Display Adapters entry > right-click on Graphics Driver and click on Update driver option.

3. On the next screen, select Search automatically for updated driver option and follow the instructions to update the Driver.
After the update process is completed, Restart the computer and see if the problem is now fixed.
5. Restore Power Plan to Default Settings
Restoring the Power plan to default settings can help, if the problem is being caused due to an unknown change or changes in Power plan settings.
1. Open Control Panel > click on Power Options.

2. On Power Options screen, click on Change Plan Settings link, located next to the current Power Plan.

3. On the next screen, click on Restore Default Settings for this plan link

After Default Power Plan Settings are enabled, you might find your computer behaving normally.
6. Disable Hybrid Sleep
Hybrid Sleep is not supported by some computers. Hence, turn-off Hybrid Sleep and see if this helps in fixing the problem.
1. Open Control Panel > click on Power Options.

2. On Power Options screen, click on Change Plan Settings link.

3. On the next screen, click on Change Advanced Power Settings Link.

4. On the next screen, expand Sleep and Sleep After entries > set Allow Hybrid Sleep on both Battery and Plugged in to Never or Off option.

5. Click on Apply and OK to save the above changes on your computer.