Throughout history, couples have gone to extraordinary lengths to choose the sex of their child. In the middle ages, women believed they could swing the odds of having a son by asking their husbands to turn their faces eastwards during sex. Others disagreed - husbands should be seduced over a cocktail prepared with red wine and fresh rabbit's womb.
If that didn't do the trick, the 18th Century French anatomist Procope-Couteau had a rather extreme measure. Men who'd "give their left testicle for a baby boy" should do exactly that, he said. He claimed the surgery was no more painful than extracting a tooth.
Even today, a quick search of the internet reveals an array of exotic solutions - from vitamins to cough syrup, to changing your underwear. The more rational among us may be sure that none of it makes any difference. Each conception is a flip of the coin: there's always an equal chance a baby will be a boy or a girl. It's purely a matter of probability - and you can't cheat lady luck.
Yet recent research shows parents may have been inadvertently rigging the odds for thousands of years - and the true factors which make a difference are stranger than anything our superstitious ancestors could have imagined.
We now know that bad weather makes for more baby girls, as does fasting for Ramadan or suffering from morning sickness. Meanwhile mothers with dominant personalities, a taste for breakfast cereal or billionaire husbands are more likely to have baby boys. Crucially, a predisposition to having more sons or daughters is encoded in our genetics - men with more sisters tend to have girls while those with more brothers tend to have boys. What's going on?
I now see that the whole problem is so intricate that it is safer to leave its solution for the future - Charles Darwin
In fact, the odds of having a boy vs. having a girl have never been exactly 50:50. Worldwide, there are around 109 boys born for every 100 girls. This might seem like a lot, but it's necessary. Men have weaker immune systems, higher cholesterol, more heart problems, a greater susceptibility to diabetes, higher rates of cancer and lower chances of surviving it. They make up over two-thirds of murder victims, three-quarters of traffic accident fatalities and are three times more likely to commit suicide. Mothers have to have a higher proportion of sons in order for an equal number to survive.
The relative odds of conceiving sons or daughters have been baffling scientists for decades. The phenomenon was particularly mysterious to Charles Darwin, who meticulously studied the proportion of male and female offspring in a number of animals.
He was convinced that the elaborate features of many male animals, such a peacock's tail, must be a consequence of a dire shortage of the opposite sex. In these species, he figured, more competition had favoured males which stood out from the crowd.
There was just one problem. In every species he studied, there were almost (but not exactly) the same number of males and females; the variation was not nearly as wide as he had expected. After failing to find any convincing evidence, eventually he abandoned the whole topic, remarking "...I now see that the whole problem is so intricate that it is safer to leave its solution for the future."
So why is the sex ratio close to 50:50, but not exactly? It was a tricky subject in need of a daring intellectual mind. Enter Robert Trivers, a renegade scientist like no other. According to his own website, he's spent time behind bars, formed an armed gang in Jamaica to protect gay men from violence and driven a getaway car for notorious political activist Huey Newton.
The Ivy League professor has been barred from university campuses, suffered mental breakdowns and broken with tradition by changing disciplines numerous times, from maths, to law, to history, to genetics. Now he's an anthropologist at Rutgers University. "Compared to many other scientists I've lived a wild life," he says.
Back in 1972, after tiring of history, he turned his attention to Darwin's problem. "I said whoa - now there's an idea worth devoting my life to," says Trivers. Together with a colleague, Dan Willard, he developed one of the most famous theories in evolutionary biology. It's known as the Trivers-Willard hypothesis and it goes like this.
Let's assume you can choose the sex of your children - and the game is to leave as many descendants as possible. You have a gamble to make. If your children are male, who knows, they might become the next billionaire tycoon, or US President (or both), with plenty of girlfriends to choose from.
If your son is a success, it could be a big evolutionary win
It's a scientific fact that high social status is attractive to women. Fertile women prefer more dominant men and the lucky few who achieve money or influence tend to marry younger, more often and have more extra-marital affairs than their peers. If your son is a success, it could be a big evolutionary win. But if he isn't, he may find himself unable to find a partner at all.
"Because women tend to marry 'up' the socioeconomic scale - it's as if women trade in their good looks to some degree - if you're a man at the bottom of the scale you're going to struggle to find someone to marry," says Trivers. Females, by contrast, tend not to face such stiff competition; they have a higher chance of securing a mate and producing some offspring, even if they will never result in as many descendants as a son.
That may sound a little bit sexist, but Trivers argues that it arises from the fact that a female invests more in the young, compared to a male who can just have sex and leave the childrearing to the mother. Consider the fact that the most prolific mother in world history was the nameless first wife of a peasant from Shuya, Russia, who lived from 1707 to 1782. In total, local records reveal that she gave birth to 69 children, which is nothing compared to the warrior Genghis Khan, who fathered between 1,000 and 2,000 children before he died in 1227. Today, he's thought to have around 16 million great, great, … great grandsons. And Khan is not the only one; a recent DNA analysis revealed at least 10 men from history who have left a legacy comparable with Khan's, including a Chinese ruler who died in 1582, and the originator of the medieval Uí Néill dynasty in Ireland.
In many animals - red deer, elephant seals, gorillas - the stakes are even higher. Successful males may have harems of hundreds of females, while low-ranking or weak males may never reproduce or die trying.
Then there's the issue of resources. Because they tend to be larger, sons require a lot more food than daughters and in many societies they'll require more education and money. To produce a son capable of becoming a dominant, high-status male, parents will need to make a big investment.
With these factors in mind, Trivers proposed that in favourable conditions, such as where the parents were high status or food was abundant, it would make evolutionary sense for parents to produce more sons. But in less favourable conditions, natural selection should favour parents who produce more daughters, since females don't face such fierce competition. Even if they aren't particularly attractive or socially successful, it's likely they'll have at least some children.
"At the time I gave the joke that this was the perfect theory because it would take 20 years to prove me wrong. But 11 years later I was proved right," says Trivers.
Back in the 80s, scientists discovered that in red deer at least, dominant females have a 60% chance of giving birth to a son. But could this also be true in humans? The first evidence came from an unlikely source. In 1958, China's ruling party announced an ambitious new project: the Great Leap Forward, which they hoped would propel the nation of peasant farmers to industrial glory in the space of a few years.
Families were ordered to abandon their farms as the country prepared to step up steel production by 30%. Gardens were turned into makeshift smelting yards as possessions - from cooking pots to tractors - were melted to artificially inflate the total.
Before long, the country was transformed - but not in the way the government had hoped. Just a year after the project began, grain output had dropped by 15%. A year after that, it dropped again. Within four years of the famine setting in, 45 million people were dead.
Nearly four decades later, economist Douglas Almond found himself poring over Chinese census records to find out what had happened afterwards. But he wasn't looking at the records of the victims - he wanted to know what life was like for their middle-aged children.
Together with colleagues from Columbia University, he compared the records of those born soon after the famine with information about the province in which their parents were born. Some areas were affected more than others, so the team were able to compare the prospects of those whose mothers had gone hungry with those whose mothers had not.
What they found was alarming. Though the children hadn't experienced the famine themselves, those from famine-stricken regions were less likely to be literate, employed, self-sufficient and tended to live in smaller homes. Women tended to marry later and men were lucky to marry at all. Finally, across the whole sample, those born to affected mothers were significantly less likely to be born male in the first place. The effect even seemed to carry over to their children, who were also more likely to conceive daughters.
To estimate the size of the effect, remember that worldwide there are around 109 boys born for every 100 girls. But between 1960 and 1963, the number of male children born in China fell to just 104 boys for every 100 girls, a difference of around 5% according to a later study on the famine. The ratio didn't return to normal until 1965.
We now know that from smoking to war, to climate change, unfavourable conditions predispose women to having more girls. On the other end of the scale, women with more dominant personalities, a diet rich in high calorie foods (such as breakfast cereal), or married to U.S. Presidents tend to give birth to more sons. For billionaire fathers, the odds of having a boy are 65%.
At this point you might be wondering why, with all these influences at work, the ratio of men to women in the world isn't wildly unequal. Surely a disaster on the scale of the Great Chinese Famine should have produced a generation primarily of girls?
According to Keith Bowers, an ecologist at the University of Memphis, there are good reasons why the population never veers too far from the gender balance. "Sons need more food than daughters, so consistently over-producing males create a more competitive family environment," says Bowers. If all parents had sons when times were good, they may struggle to find a mate or territory when they grew up. Meanwhile, those with a genetic predisposition to over-produce daughters while everyone else is having sons would have a big advantage. "Over time you'd expect roughly equal numbers of males and females to be born," says Bowers.
According to Corry Gellatly, an evolutionary biologist at Utrecht University, this natural rebalancing may already be happening. In China, where there is a cultural preference for boys, the introduction of the one-child policy led to a substantial spike in the number of girls being aborted. As you would expect, between the 1980s and 2000s - when the policy was in full force - the majority of babies born were boys.
But in families which had more than one child (this was allowed in certain circumstances, such as if the parents were poor farmers from the deep mountains, or if the parents themselves were only children), the firstborn child was significantly more likely to be born a girl compared to the average.
This may be because parents are more likely to leave the gender of their first child up to chance - they'd care more if the second child was another girl - and with an overpopulation of boys, the odds were tipped in favour of the minority sex. Ironically, in trying so hard to have only boys, the population may have made it more likely that they would conceive a girl.
Alas, this will not be able to fully rebalance the ratio of boys and girls . As of 2015, Chinese men outnumber women by 60 million. It's been estimated that by 2030, one in four Chinese men will be unable to marry. In societies with an over-abundance of males, there may be unsavoury consequences - from higher rates of domestic violence to organised crime, to murder. Some have even suggested that this growing pool of frustrated bachelors may be attracted to the military, with the potential to trigger large-scale international conflicts.
Perhaps it's time to put the cereal away, leave your testicles alone and accept that, in the end, the chances of having a boy are - and should be - roughly 50:50.
If you liked this story, sign up for the weekly bbc.com features newsletter, called "If You Only Read 6 Things This Week". A handpicked selection of stories from BBC Future, Earth, Culture, Capital, Travel and Autos, delivered to your inbox every Friday.