According to the famous 'drunkard's walk' problem, the distance a drunk, making random left and right turns, gets from the lamp post is his typical step size times the square root of the number of steps he takes. If his step is one meter long, it will take him 100 steps to travel 10 meters from the lamp, and 10,000 steps to travel 100 meters.
For the sun, we know how far we want to go to get out....696,000 kilometers, we just need to know how far a photon travels between emission and absorption, and how long this step takes. This requires a bit of physics!
The interior of the sun is a seething plasma with a central density of over 100 grams/cc. The actual distance that an x-ray or gamma-ray photon can travel is between 1 centimeter and 1 millimeter. Very approximately, this means that to travel the radius of the Sun, a photon will have to take (696,000 kilometers/1 centimeter)^2 = 5 x 10^21 steps. Now, light travels 3 x 10^10 centimeters/second, so this will take, 5x10^21 steps x 3 x10^-11 seconds/step = 1.5 x 10^11 seconds. Since there are 3.1 x 10^7 seconds in a year, you get about 4,000 years.
Some textbooks refer to 'hundreds of thousands of years' or even 'several million years' depending on what is assumed for the mean free patch.
There are many approximations that enter into these calculations. The interior of the sun is not at constant density so that the steps taken in the outer half of the sun are much larger than in the deep interior where the densities are highest. Note that if you estimate a value for the mean free path that is a factor of three smaller than 1 centimeter ( i.e 3 millimeters), the time increases a factor of 10 to 40,000 years.
X-rays and gamma rays take different step lengths because the opacity of the Sun is different to these two forms of radiation emitted by the core.
Typical uncertainties based on this 'order of magnitude' estimation can lead to travel times 100 times longer or more, and travel times approaching a million years.
Most astronomers are not too interested in this number and forgo trying to pin it down exactly because it does not impact any phenomena we measure with the exception of the properties of the core region right now. These estimates show that the emission of light at the surface can lag the production of light at the core by about 100,000 years or so.
The point of all this is that it takes a LONG time for light to leave the sun's interior!!