It is quite common for babies to come out of the womb with blonde hair, only for it to darken to brown later in life. The baby isn't blonde, it just looks blonde right now.
Same with gender. Doctors observe a flavor of genitals, make a reasonable assumption, and legally assign the gender which seems appropriate.
One cannot ask a baby what social role they would like to have. Typically, in approximately 97-99% of cases, that aligns with the genitalia. So no, no coin flip. It's typically done by looking at genitalia. You'll be right almost always.