The Kibble-Zurek scenario for nucleation of topological defects of the order parameter during a symmetry-breaking phase transition in a. superconducting ring is investigated. Solving numerically the set of time-dependent Ginzburg-Landau (TDGL) equations coupled with the temperature diffusion equation, we predict spontaneous generation of the magnetic flux inside the superconducting ring. Josephson weak links in the ring increase the magnitude of the spontaneously generated magnetic flux.