Is a pokemon's gender determined before it hatches from its egg?

At what point is a pokemon's gender (or other stats) determined when it originates from an egg? Is it determined as soon as the egg is added to the player's inventory, or is it only determined when the egg hatches? (Or is it determined at some other time?)

I am particularly interested in the answer for Sun and Moon.


Solution 1:

Everything about Eggs is determined when you pick up the egg.

Additionally, nothing else (not even resetting) affects the Random Number Generator used for Eggs in Sun/Moon, which means unless you actually pick up (or decline) an Egg, soft resetting does nothing and you will pick up the exact same egg. In order to actually soft reset for Eggs, decline the Egg right after resetting, then save again so you can repeat the process until it corresponds to your wishes.

Since the RNG is deterministic when you know how it works (and there are programs that do), this allows precise control over your Eggs.