Life for the first humans was undoubtedly brutal. They were in a constant struggle to stay warm and fed, while warding off predators and enduring an unforgiving climate. To survive, they needed to be tough, intelligent, and resourceful.

And happy.

Evolutionary psychologists theorize that happiness was an adaptive advantage. Happiness aided our survival in all sorts of important ways—it made us fitter, more attuned to our environment, more social, more energetic—and because happy people were more apt to survive, they were more likely to pass on their happiness genes. Like all evolutionary adaptations, happiness first occurred as a genetic variation, but persisted because it helped us stay alive.

Happiness describes a range of emotions, from pleasure to glee to contentment. But in the context of evolution, the happiness that proved most useful was the tingle of delight we feel when we’re met with unexpected good fortune.

That sensation is fueled by dopamine, a chemical in the brain associated with motivation and reward-seeking behavior. Scientists who study dopamine know it increases when we’re pleasantly surprised, but not when we’re merely satisfied. An unexpectedly good result, like surprising yourself on a test, will send dopamine level soaring. Biting into a chocolate bar won’t create the same glow, because it holds no surprise. Dopamine heightened our ancestors’ awareness and focused their attention on what they wanted; they craved the feelings it produced and it guided them toward comfort and love, conditions that help make us human.

To survive, early humans needed to be tough, intelligent, resourceful—and happy.

“Dopamine isn’t interested in what’s expected; it’s interested in what’s surprising so it can help you learn about your environment,” says Robb Rutledge, a researcher at University College London. “Your happiness depends on whether things are getting better or worse, and knowing that can help you make better decisions in the future.”

Happiness and early humans

In early human societies, where communities were small and life could be perilous, happiness could make the difference between who survived and who didn’t.

The happier people would not only be more likely to attract a mate, but also to develop the skills that would help them survive. Workers in a good mood are more creative and energetic, studies show. Happy people are more attractive to others and develop stronger relationships, which would be essential for making friends and allies to help in adverse conditions.

As a result, happy survivors would be more likely to pass along the genes that produce happiness. Research confirms that much of happiness is hereditary: Studies of identical twins raised apart showed 50% of an individual’s happiness is due to genetic factors.

“There’s lots of research that shows that happiness has benefits,” said Sonya Lyubomirsky, a psychology professor at the University of California-Riverside and the author of The Myths of Happiness. “It’s not just something that makes us feel good but helps us succeed in in life. People who are happy make more money, are more likely to get married, have stronger immune systems, and more friends.”

Some manifestations of happiness that benefitted early humans are now detrimental to our emotional health.

So it makes sense that as evolution favored happiness, we evolved to be happy. Studies show that, on the whole, humans are pretty happy as a species. Researchers compiled the results of nearly 1,000 surveys—which cumulatively polled 1.1 million people around the world—and found most people consider themselves to be more happier than not, ranking their happiness, on average, just above seven on a 10-point scale.

That’s true of people who have little obvious reason to be happy. In one survey, the pavement dwellers of Calcutta—a homeless population living in some of the world’s most miserable conditions—considered themselves satisfied with their lives. Even prison inmates eventually come to describe themselves as happy, after an initial period of unhappiness. The fact that so many of us are happy suggests it’s how we’re built.

But while happiness has benefits, so does discontent. Both were necessary for early human survival, Lyubomirsky says. It’s possible that less-happy people may have had other traits, like intelligence, that helped them survive and pass along their non-happy genes.

Schadenfreude as survival mechanism

Similarly, some manifestations of happiness (pdf) that benefitted early humans are now detrimental to our emotional health.

Survival back then was often binary, where one person’s gain was another’s loss, particularly when it came to finding mates. As a result, we’re primed to take pleasure in the downfall of others, the form of happiness we know as schadenfreude, according to David Buss, a psychology professor at the University of Texas.

While schadenfreude and similar emotions might have helped our ancestors survive, they’re less useful now.

“The way that evolution by selection works is that it’s inherently competitive,” says Buss. “The two ways to achieve it are to enhance ourselves or to facilitate our enemies’ falling. It’s not a rosy picture of human nature, but it’s who we are.”

While schadenfreude and similar emotions might have helped our ancestors survive, they’re less useful now, when cultural mores urge us to be charitable toward rivals, and frown upon delighting in others’ misery.

The flip side of the happiness early humans enjoyed from their success was the unhappiness they found in failure. That, too, was evolutionarily helpful, Buss says. If they experienced jealousy, distress, or horror, it would spur them to improve their conditions and return to a state of happiness.

The power of pain

For most of us, unhappiness is a more powerful driver than happiness. Because unhappiness motivates us to make changes, we’re hard-wired not to remain too happy for long.

In a series of experiments, psychologists Daniel Kahneman and Amos Tversky demonstrated that we feel the sting of loss more than we enjoy the gain of the same amount. “Pain is more urgent than pleasure,” they wrote in an influential1991 paper that helped Kahneman win a Nobel prize.

That’s probably because unhappiness was ultimately more useful for survival. Unhappiness, and its cousins misery and pain, could prevent starvation and dismemberment, while happiness was a more subtle goad toward the better things in life.

Unhappiness, and its cousins misery and pain, were more useful for survival.

As a consequence, no matter what amount of good fortune we encounter, we return to a base level of happiness. That’s why lottery winners don’t remain ecstatic with their winnings, and why buying a new car doesn’t solve our long-term unhappiness. Psychologists call this phenomenon the “hedonic treadmill.”

Living in a state of perpetual happiness would actually have been dangerous for early humans. If humans reacted to every situation with the same cheerful attitude—treating enemies the same as friends, accepting barren environments as if they were fruitful—they would not last long, wrote Jerome Barkow, an emeritus professor of sociology and anthropology at Dalhousie University in Halifax, Nova Scotia. “You wouldn’t be alert, you wouldn’t be competitive,” he said.

The pursuit of happiness

Not all forms of happiness can be explained by evolution, particularly more recent manifestations. Culture plays an important role in adaptation, as different groups adopt different behaviors that help them navigate their unique worlds, Barkow said.

Likewise, understandings of happiness have changed over time. How different societies viewed happiness helped shape their culture and survival strategies.

In ancient China and Greece, happiness was associated with luck and good fortune, a result of external factors that couldn’t be controlled. Over time, as humans gained more mastery over their living conditions, happiness came to be perceived as something an individual could control.

In a 2015 study, researchers led by Shigehiro Oishi of the University of Virginia looked at how happiness was used in every State of the Union address made by US presidents to that point. Early presidents talked about “happiness” as good fortune and prosperity, while more recent uses took on the sense of personal well-being and life satisfaction. The authors theorize that as the US and its citizens grew more prosperous, “people’s perception of personal control in life also increased, and made the fortune-based definition of happiness look obsolete.”

In the same study, the authors also examined the definitions of happiness in 30 modern countries and found that the more happiness was associated with good luck, the less happy a nation is. Those countries where happiness is seen as a function of luck are also farthest from the equator, where living conditions were historically more rugged and food more scarce. Happiness, the researchers argue, might once have been an infrequent occurrence in those places, and that lingers in the cultural memory of a nation and its people.

In countries where happiness is understood as the result of individual actions, such as the US, people are more likely to make decisions to pursue it. In countries like Japan or Russia, where happiness is understood to be reliant upon fortune, decisions are more likely to be made for reasons other than to maximize happiness.

Ultimately, human happiness isn’t the result of any plan or grand strategy. We’re not programmed to be happy. Like all adaptations, it’s the result of countless mutations, over millennia of trial and error. Our happiness is the product of the same forces that created tulips, giraffes, and viruses. It just happened that our chances of survival were enhanced by finding satisfaction in a job well done.