The magnetic field of the Earth, and of other planets that have magnetic fields, is generated by dynamo action in which convection of molten iron in the planetary core generates electric currents which in turn give rise to magnetic fields [REF].
In some simulations, this leads to an instability in which the magnetic field spontaneously flips over into the opposite orientation.