Human beings like to form groups. Whether it’s a collection of fans rooting for their favorite team in the Super Bowl or millions of people who have coalesced into a single country, our civilization has largely been defined by people drawing a line between “us” and “them.” A new computer modeling study suggests that it doesn’t take much to form these assemblages. Indeed, they can emerge via just two simple rules.

“It’s a very nice little model,” says Robin Vallacher, a psychologist at Florida Atlantic University, Boca Raton, who was not involved in the research. It shows “how something so complex and so troublesome in the modern world can be understood in terms of some very basic minimalist processes.”

To create the model, a team led by Kurt Gray, a psychologist at the University of North Carolina, Chapel Hill, and David Rand, a psychologist at Yale University, started with dozens of identical virtual “agents,” representing people, that repeatedly interact with each other. At first, all agents are equally close to each other, but over time some form tight bonds while others form rivalries. The model determines the level of closeness between two agents by making them play prisoner’s dilemma each time they meet up—a game in which acting selfishly scores you the most points in the short term but prevents long-term cooperation and mutual benefit. When two agents cooperate, their closeness increases; when they don’t, it decreases. Their responses are determined by chance, but closeness can bias them toward cooperating or defecting.

To allow these cooperating agents to form a larger group, the model lets them “gossip” and share alliances. After two agents interact, they each adjust their closeness to third parties to resemble their interaction partner’s closeness to those parties. So if agents A and B are close, and agents B and C are also close, A will become close to C, too.

In the end, group formation came down to two simple rules: the laws of reciprocity and transitivity. That is, you scratch my back, I’ll scratch yours; and a friend’s friend is my friend. Even with diverse levels of reciprocity and transitivity, clusters reliably form, the team reports in an upcoming issue of Psychological Science. Groups of agents who are very close to each other and very far from everyone else emerge. Those who are close frequently cooperate, while those who are distant compete; they avoid interaction and act selfishly when they do. The researchers also found that different levels of reciprocity and transitivity don’t affect group size and number very much, and that increasing the population makes for a few big groups rather than lots of little ones. (You can run the model yourself here.)

Most theories of group formation argue that people cluster together based on pre-existing similarities—shared ethnicity or beliefs or a favorite team. “If you asked the average person on the street or even many psychologists to pick one thing that could explain how groups form,” says Lisa Barrett, a psychologist at Northeastern University in Boston who was also not involved in the research, “I’m not sure they would pick helping and harming. I think that’s pretty significant.”

“It may be that groups are maintained and elaborated with more complicated processes related to identity and cultural practices,” Barrett adds. “But what this shows is that simple affective reactions to helping and harming are sufficient to model group formation.”

The findings resemble real-world clustering behavior among nearly homogeneous groups: Tanzanian hunter-gatherers, executive MBAs at a mixer, and monks in a monastery. “We talk about post-racial, post-religious America—the idea that we can put people into a melting pot and dissolve differences to make people a unified people,” Gray says. “But the thing is that people get into groups naturally, even if they literally don’t see race, or even understand that there is an ‘us’ versus ‘them.’ You can just program agents with payback and a bit of gossip and then boom! Instant strife.”