When it comes to human circumstances, our perception of "good" and "bad" is largely relative (note: this doesn't mean morality is relative). For example, rain is considered "good" during a drought but "bad" during a flood. An American at the poverty line would be considered "rich" in Liberia. Even universally bad things, such as the death of children, which truly is a tragedy, is only considered tragic because humans generally live longer and develop more. If humans lived as long as bristlecone pine trees, even 200 years would seem tragically short. If our average life span was that of mayflies, a few years would be a very long life.
We also tend to perceive the present relative to the recent past [or at least our distorted perception of it]. We are happy when our situation improves and unhappy when it worsens. Otherwise, we perceive the present relative to [a somewhat idealized version of] what we think our lives could or should be like. Thus, we often are not content when our situation isn't improving. In mathematical terms, human happiness is closely associated with the first derivative (i.e., rate of change with time) of the "goodness" of our circumstances. We're happy when the derivative is positive (i.e., improving with time), sad or unhappy when it's negative, and discontented when it's near-zero. The Bible includes numerous examples of this concept, especially in the books of Exodus and Numbers.
The Israelites were slaves in Egypt for many years. After being miraculously freed (Exodus 14), they were elated and sang praises to God (Ex. 15). But eventually, after they started getting short on food, many started wishing they had never left Egypt. Then God gave them an endless supply of food (manna; Ex. 16). That satisfied them for a while, but they eventually grew tired of eating it. Again they started wishing they had never left Egypt, remembering the diverse food they had there (Numbers 11). The absolute "goodness" of their circumstances (C) is plotted on the time series graph below:
The derivative aspect of happiness explains a lot about life. It explains why drugs and other forms of instant gratification don't ultimately make us happy. It explains why people always try to acquire more money and more things despite having more than they'll ever need. It explains why it's so much more painful to have something and lose it than to have never had it. It might also explain the Bible's moral teachings, which generally encourage selfless and healthy behavior that isn't quite as enjoyable in the short-term as the things it discourages or forbids. And I believe it helps us better understand why a loving God would create a harsh, imperfect, broken world and ask us to fix it.
It's natural to think a loving God should've made the world a happy, pain-free place where everyone lives at least 80 years. But such a utopia only seems good relative to our world. If the utopic world was the only one we ever experienced, it wouldn't necessarily seem any better because we'd perceive "good" and "bad" relative to that world. In mathematical terms, the value of C would be higher but dC/dt wouldn't be any different. We'd be like the Israelites after the Exodus, finding new [and more petty] things to be unhappy about.
So why do bad things happen? If "bad" is indeed relative, bad things happen simply because good things happen. The only way to eliminate the "bad" is to eliminate the "good", which implies a constant C(t) that is the same for everyone. That would make dC/dt = 0, which, as the ancient Israelites can attest, seems quite attractive when dC/dt < 0 but not so much after you have it for a while.
To be clear, I'm not saying God created the world to maximize human happiness in it. If that was his goal, he didn't do a very good job with it. But I think the nature of human happiness points us to something deeper. It suggests that a truly good world is one that is always improving. That's the kind of world the Bible promises, and I believe history shows that it's the kind of world we have. An improving world means there are things that need improving, which can be uncomfortable, painful, even horrific at times. It's also the kind of world that enables innovation, compassion, justice, forgiveness, mercy, hope, and perhaps even purpose. What is your purpose in life? And what would your purpose be if the world had no room to improve?