The Talmud discusses a case where a surviving brother went ahead and did Yibum with a widow shortly after his brother died. Then they realized that his brother’s widow, now his wife, was pregnant, and it is not clear who the father is.
The first child is certainly no problem. Either it belongs to the deceased and no Yibum was necessary (but atonement by the brother and widow is needed for their mistake) or it was fathered by the surviving brother and the Yibum was appropriate since the dead brother had no children. However, the new couple cannot continue their relationship due to the chance that the Yibum was not required, which would make the marriage forbidden instead of a mitzvah.

In Jewish culture, and all successful traditional human societies, it matters who the father is. A strong father figure is the foundation of every happy family. The Gemara brings a shocking teaching:
Rabbi Eliezer ben Ya’akov says: A man who engaged in intercourse with and impregnated many women, but does not know with which women he had intercourse, or similarly, a woman with whom many men had intercourse, but she does not know from which man she became pregnant, since the identities of the parents of children are not known, it could emerge that a father marries his own daughter, and a brother marries his own sister.
The entire world could become filled with bastards (mamzerim). And concerning this, the Bible states: “Lest the land become full of lewdness” (Leviticus 19:29)…
He also taught: a man should not marry a woman in this country and go and marry another woman in a different country, lest a son from one and a daughter from the other unite with one another, and it could emerge that a brother marries his sister.
This is a warning against the perils of promiscuity, in particular for creating children of unknown parentage. That situation would create even more sins, as people unknowingly married forbidden family members.
The Talmud contrasts this to the behavior of prominent sages:
But didn’t Rav, when he happened to come to Dardeshir, make a public announcement saying: Which woman will be my wife for the day, i.e., for the duration of his visit? And also Rav Naḥman, when he happened to come to Shakhnetziv, made a public announcement saying: Which woman will be my wife for the day?
We discussed this Gemara the other time it appears in Shas, Yoma 18. The concept here is that our sages did not want to be tempted to sin while away from their wives. They wanted to be married to another woman while out of town, in order to have a healthy outlet available for sexual thoughts or temptations.
They sent word that they were coming and asked if a woman was willing to be available to be married, if the need arose. The commentaries on the Gemara (Tosafot) point out that the sages did not typically sleep with these women, but wanted to have that option available. Just having the option helps a man to avoid improper thoughts.
If they ended up staying longer than intended, they would indeed sleep with these women. Since these were well known rabbis, it would be common knowledge that the children were theirs, avoiding the situation of doubtful parentage that Rabbi Eliezer warned of. In addition, Tosafot brings a tradition that if the temporary wife became pregnant, the sage would bring her home to be able to take care of her and the child.
For a general discussion of polygamy, see Kosher Polygamy
While this behavior by religious leaders may sound outrageous in our day, this was an accepted method for men to cope with their passions without resorting to spilling seed, forbidden activities or creating children without parents.
Some religions demonize normal human desires, while other philosophies teach people to indulge without thinking of the consequences. Perhaps there is a middle ground that helps people find a positive outlet for physical urges while maintaining social standards of parentage and exclusivity.