Henry VIII famously went through six wives, and executed two of them, in his despairing attempts to produce a male heir. His approach was extreme, but Henry was far from alone in his desire to choose the sex of his children.

Parents-to-be have tried all manner of things to ensure they have a boy or girl. In ancient Greece, men lay on their right side during sex to guarantee a boy. In 18th-century France, it was believed that a man could tie off his left testicle for the same result.

These days, books like How to Choose the Sex of Your Baby recommend that couples who want a boy try to conceive on the day of ovulation or the day before. The idea is that sperm carrying the Y-chromosome, which produce males, move faster than sperm carrying the X-chromosome, but don't last as long.

There is no proof that any of these tricks work, and many of them now look outright ridiculous. But there is little doubt that many animals unconsciously alter the sex ratio of their offspring, producing extra boys or extra girls. So why would animals ever choose to produce more of one sex? How do they do it? And can humans do it too?

The story begins in the 1930s, when biologist Ronald Fisher tried to explain something that had baffled Charles Darwin. Sexually reproductive animals usually produce roughly equal numbers of sons and daughters, but no one knew why.

If there are too many males, parents should produce more daughters

Fisher argued that the sex ratio ought to be self-correcting. If there are more males than females in a population, on average each male will get less than one mate. That means it would pay parents to produce female offspring, as that would give them a better chance of having grandchildren and thus preserving their genes.

So if there are too many males, parents should produce more daughters. Similarly, if there is an excess of females, on average each male gets more than one mate, so parents should produce more sons. For this reason, the sex ratio should always revert to one-to-one.

It's an elegant theory. There's just one problem. Biologists have shown that it doesn't always hold true. In particular situations, parents are better off producing higher proportions of one sex. For instance, fig wasps mostly produce daughters.

Pollinating fig wasps have mutually beneficial relationships with fig trees. The wasps pollinate the figs, allowing them to reproduce, and in return the figs protect and nurture the wasps' young.

Male wasps compete with their brothers for mates – which are often their sisters

A month or so before a tree's figs ripen, female wasps are attracted to the fruit and enter it through little holes. These soon close up, trapping the females inside. The wasps pollinate the fig flowers, which grow together with the seeds inside the fruit, then they lay eggs and die. When the eggs hatch, wingless males mate with females, and then the females leave.

Most species of fig wasp produce precious few sons: sometimes as little as 5% of each brood is male. That might seem odd, but it's for good reason.

Because all mating takes place within one fig, male wasps compete with their brothers for mates – which are often their sisters. Males that fail to mate might as well not have been born.

"If you produce fewer sons, there is less competition for each brother, and you increase the average value of each son," says Stuart West of the University of Oxford in the UK. What's more, the males often mate with their sisters, so "if you're producing more daughters, you're also producing more mates for your sons."

This is precisely as evolutionary biologist William Hamilton predicted in the late 1960s. What's more, it turns out that if the competition between sons is more intense, parents are likely to produce even fewer of them.

The parasitoid jewel wasp Nasonia vitripennis is an extreme example. These wasps lay their eggs inside blowfly pupae, but apart from that they reproduce much like the fig wasps. In 2008 West's team studied the pupae they used, and found a pattern. "Females shifted their sex ratios according to the number of eggs other females had laid on the same pupa," he says.

If a female wasp finds a pupa that has no eggs from other females, she biases the sex ratio heavily toward females. But if other females have already laid eggs in the pupa, she produces a brood with a less female-biased sex ratio. There are already unrelated females in the pupa that her sons can try to mate with, so the competition will be less intense.

In other species, relatives don't compete for mates, but for food.

In 1978, Anne Clarke of Binghamton University in New York studied the reproductive habits of thick-tailed bushbabies, a species of primate from sub-Saharan Africa. She found that they produce more males than females, and argued that this was a result of competition for food.

Sometimes, breeding adults recruit their offspring to help them raise the next brood

When young bushbabies reach adulthood, they leave their mothers behind. But females don't go as far away as males do, so daughters end up competing with their mother for food. As a result, bushbaby mothers have a reason to prefer sons.

There is support for this idea. In 2008 Joan Silk, now at Arizona State University in Tempe, surveyed the sex ratios of 102 primate species. She found that birth sex ratios were slightly skewed in favour of the sex that disperses from the mother's territory.

But this isn't always true. Sometimes, breeding adults recruit their offspring to help them raise the next brood. This can completely reverse the pattern.

In 2008, Silk worked with J Weldon McNutt of the Botswana Predator Conservation Trust to study sex ratios in African wild dogs. They found that their litters include more sons than daughters, and argue that this may be because of how they disperse.

Females must also consider how much food is available

Sons remain in the same pack even after they are mature, and provide food for the mother and her subsequent offspring, whereas females leave earlier. "Males are better helpers than females, so it makes sense that the wild dogs' sex ratios tend to be biased toward males," says Silk.

The male bias was most extreme for younger mothers with smaller packs. "That's when males are most useful," says Silk. 

This is all getting quite intricate, and there's another twist to come. Females must also consider how much food is available when deciding what sort of offspring to have.

The most striking example of this is the Seychelles warbler. These birds are only found on a handful of tiny islands in the Indian Ocean. Breeding pairs stay together in the same territory and produce one offspring per year. Young males usually scatter, while females stick around to help their mothers.

Seychelles warblers adjust their sex ratio in response to local food circumstances

You might assume they would always produce more of the helping sex. But that’s not always the case, says Jan Komdeur of the University of Groningen in the Netherlands. Instead, what they do depends on where they live.

On high-quality patches, where insect prey is plentiful, helpers are beneficial. But on low quality territory, where food is scarce, they provide increased competition for food.

In 1996, Komdeur discovered that breeding pairs produce 90% daughters on high-quality territories and a similarly high proportion of sons on low-quality territories. In a follow-up experiment, he moved breeding pairs from low-grade patches to high-grade patches, and found that they switched from producing 90% males to 85% females. "This was really clear experimental evidence that Seychelles warblers adjust their sex ratio in response to local food circumstances," says Komdeur.

What's more, the parents' sex-selective policy pays off.

In a study published in 2008, Komdeur swapped nestlings between breeding pairs with no helpers, and then observed these birds for three years. Parents given the "correct" offspring had more grandchildren.

Foster daughters can become co-breeders

For instance, parents on low-quality patches produced more grandchildren if they had foster sons than pairs on the same patch raising foster daughters. The reverse was true on high-quality territories. Parents raising foster daughters where there was lots of food did better than those raising foster sons on the same patch.

"On territories with lots of food, foster daughters can become co-breeders, laying additional eggs in their mother's nest without harming their mother's offspring," says Komdeur. This boosts the number of grandchildren, but it's only possible when food is plentiful.

The warblers are adjusting their offspring sex ratio in response to changes in their living conditions. Other animals go a step further, and change their offspring based on their own condition.

Tim Clutton-Brock of the University of Oxford in the UK once spent several years studying red deer females on the Scottish island of Rhum. In 1984 he found that high-ranking females produced a high proportion of sons, while low-ranking females produced more daughters.

If the mother is in bad shape she should produce more daughters

This may be because the high-ranking females are in better condition. A decade earlier, biologist Robert Trivers and mathematician Dan Willard had suggested that females in good shape should produce more sons. They reasoned that sons have to compete with other males to mate, so only high-quality sons sire lots of offspring. As a result, when a mother can afford to plough plenty of resources into her offspring, it pays to have sons.

On the other hand, if the mother is in bad shape she should produce more daughters, because even females in poor condition will reproduce. When mothers can't afford to invest heavily, the less costly sex represents a safe bet.

The female deer were choosing their offspring's sex based on their own condition. But it can also pay a female to make similar decisions based on the quality of her mate.

The idea is that a male's attractiveness is potentially a strong indication of his genetic quality. In turn, his sons benefit from that, either because they inherit his good genes or because he does a better job of caring for them. This means that the male offspring of attractive males should themselves produce lots of offspring.

Animals can control the sex ratio of their offspring

"If a female mates with a very attractive male she should produce sons, because they too will be attractive and get lots of mates," says Tim Fawcett at the University of Bristol in the UK.

This seems to play out in some birds. In 1999, Ben Sheldon of the University of Oxford and colleagues demonstrated that female blue tits who mated with males that had brighter crown feathers produced a high proportion of sons, compared to females who mated with dull, low-quality males. When Sheldon blocked the ultraviolet light reflecting off the attractive males' crown feathers, the females started to favour daughters.

All this tells us that animals can control the sex ratio of their offspring, even when the factors influencing their decision are quite subtle. But that raises another question; how do they do it?

For ants, wasps and bees, it's perfectly clear.

Their DNA is stored in long strands called chromosomes, one of which controls sex. An individual's sex is determined by how many copies it has. Unfertilised eggs, which only have one copy, become males. Fertilised eggs, which have two copies, become females. So by controlling whether eggs get fertilised, females can control the sex of their offspring.

Under this system, it's pure chance which kind of sperm fertilises the female's egg

However, in mammals and birds the mechanisms are more mysterious. "What individuals should do in terms of biasing sex ratio is well-established, but how they actually do it is much less well-known," says Fawcett.

Unlike wasps and bees, mammals and birds determine the sex of their offspring by combining different types of chromosomes. In mammals, females have two identical "X" chromosomes, while males have one "X" and one "Y". Males produce equal numbers of sperm with an X chromosome and sperm with a Y.

Under this system, it's pure chance which kind of sperm fertilises the female's egg, so the sex ratio should be 1:1. But the evidence is clear that the females can skew it somehow.

One possibility is that circulating hormones in the breeding female, known to be sensitive to environmental conditions, play an important role. In 2006, Marion Petrie at the University of Newcastle showed that the sex ratio of the offspring of Japanese quail varies with the concentration of the stress hormone corticosterone. She also found that artificially elevated corticosterone levels resulted in significantly female-biased sex ratios.

Females might selectively abort embryos of a particular sex

Blood glucose levels may also have an influence. A 2008 study found that female mice with experimentally lowered glucose levels produced a higher proportion of daughters than mice with normal levels of glucose.

Alternatively, females might selectively abort embryos of a particular sex. They could also starve particular eggs of provisions. There are many possibilities, and no reason to think that all animals use the same one.

Finally, do any of these mechanisms work in humans? Could we succeed where Henry VIII failed?

A handful of studies have looked at human sex ratios. In 2009, Thomas Pollet of VU University in Amsterdam and colleagues investigated the sex ratio bias in the children of 95,000 Rwandan women, many of whom lived in polygamous households where the man has several wives. They found that low-ranking wives produced a higher proportion of daughters than high-ranking wives or women in monogamous relationships.

Billionaires produce a higher proportion of sons than daughters

In 2012, Shige Song at the City University of New York showed that Chinese women gave birth to a significantly higher proportion of girls than boys in the two years following the Great Famine of 1959-61, during which 30 million people died of starvation.

This suggests that, if food is scarce, humans respond by producing more daughters, who are likely to reproduce even if they become weak adults. This is just like other mammals.

On the flipside, according to a 2009 study, billionaires produce a higher proportion of sons than daughters, and those sons produce more grandchildren than do daughters.

This is all about the good of your genes

If these studies are to be believed, humans have the same ability to skew their offspring's sex ratio as other animals. But it doesn't work the way many of us want it to. Whatever is happening in these studies, it is unconscious – and it's probably more to do with the mother than the father.

It's also got nothing to do with personal preference. You may intensely desire to have a son or daughter for personal reasons, but this is all about the good of your genes and the ability to produce successful children and grandchildren.

Your conscious preferences may be strong, but your genes are stronger.