
The early sages (the Tanaaim) ruled that prayer is not useful for changing reality after it has been established, and such prayers are for naught. Thus, for example, a man whose wife is pregnant and who wishes to have a son should not pray “May it be Your will that my wife bears a son,” for the fetus’s sex is already established within the woman’s womb. The scholars asked: Leah, when she was pregnant for a seventh time after having borne six sons, did not want to bear another son so as not to minimize Rachel’s allotment of the twelve tribes. Therefore she prayed that she would bear a daughter, and the sex of the fetus immediately changed within her womb, becoming a female instead of male. This shows that prayer does help, even after the reality has been established. Answer: This was a miracle and we do not learn from miracles. Another answer: Leah prayed that she would bear a daughter during the first forty days after conception, and at this point the fetus’s sex is not yet established. As the early sages ruled, the first three days from sexual relations one should pray that the semen does not spoil, from three days to forty one should pray for a son. The scholars continued to ask: The fetus’s sex is established during sexual relations, as is the opinion of Rav Yitzchak, who said that if the man climaxes first the fetus will be female, and if the woman climaxes first it will be male. This means that the sex of the fetus was established during the sexual relations and prayer will be of no help. Answer: If the couple climaxed simultaneously the sex is not established for forty days, and in this case prayer helps. But if the man or the woman climaxed before the partner, there is no reason to pray, for the sex of the fetus has already been established.
(Babylonian Talmud, Tractate Berachot 60a)