This may sound like a silly (and obvious) question; do you have anything on your finger? Sweat, grease, water, ect.? I've run into problems where my finger wasn't clean enough for it to work.
The thing is, if it is random then it's hard to replicate and therefore hard to troubleshoot. The only thing I can suggest right now, is the next time it happens, note what you were doing previous to the failure. Things like which apps, were you taking a photo, talking on the phone, etc.
It may seem like a pain in the backside, but it can help narrow down a cause.
Also, you can wipe the cache partition. It doesn't actually delete any data. Here are the steps for wiping the cache partition.
https://www.technobezz.com/wipe-cache-partition-samsung-galaxy-s8/
IF you still can't narrow down the cause, then, AS A LAST RESORT, you can try a factory reset. Again, that is AS A LAST RESORT. If it is OS/driver related, doing a factory reset will install a fresh copy of the drivers and MAY clear up the problem.
If all else fails, then you may need to replace the phone.
I hope this helps.