The screen doesn't randomly turn off: it turns off after a fixed time. You can change how long before the screen turns off in the main device settings: Display → Sleep. This applies regardless of what app is in the foreground.
If you like, you can use a third-party automation tool such as Tasker or Llama to change the screen timeout according to what app you're using. This question explains how.
Samsung phones also have a feature called "Smart stay". This activates the front-facing camera just before it turns the screen off, to check whether you're looking at the screen: if so, it leaves the screen on for a while longer. This isn't a standard Android feature: it was developed by Samsung and only available on their phones.