The sun is a ball of hydrogen so big that the gravitational pressure at the center strips electrons from the hydrogen atoms and pushes the protons so tightly together that they stick to each other. The "sticking" eventually creates helium and also releases energy in the form of gamma-ray photons. Those photons make their way through the particles in the sun, losing some energy along the way and finally making their way out of the sun as x-rays, infrared and visible light. The path from the center to the emergence from the sun takes many steps and many years.
The creation of helium from hydrogen in the core of the sun is a three-stage process that directly releases one gamma ray and indirectly releases another. Gamma rays are electromagnetic radiation, just like microwaves, radio and light waves, which means they travel at the speed of light: 300,000 kilometers per second (186,000 miles per second). The sun has a radius of about 700,000 kilometers (435,000 miles). So you could reasonably expect a gamma ray to get outside the sun about 2.3 seconds after it is created. But that doesn't happen.
In the core of the sun the protons and helium nuclei are so thick that an emitted gamma ray can't get very far before it is absorbed. If you imagine that a gamma ray is emitted right at the center of the sun then it will start out heading right for the surface. When it crashes into a proton the result of the collision is a proton with extra energy. The proton gives up that extra energy by emitting another gamma ray photon. But this one could head in any direction -- even right back where it started from. And so it goes, with the gamma ray heading from one collision to another, changing its direction each time it is absorbed and re-emitted.
Sciencing Video Vault
The Random Walk
Imagine there's a guy so drunk that he needs to hold on to a light post to stand up. He wants to get to the next light post, just 10 steps away, but he's so drunk that he can't walk in a straight line. Heck, he's so drunk that after he takes one step his next step could be in any other direction. That's what physicists and mathematicians call a "drunkard's walk" or "random walk" problem. The question is, how long will it take that guy to get from one lamppost to the next? The answer is that if his starting point and ending point are separated by 10 steps, it will take him -- on average -- 100 steps to get there -- that's 10 squared. That's the same situation a gamma ray faces in the core of the sun.
When you're trying to solve a random-walk problem, the most important thing you need to know is how big the steps are. There are two problems with figuring that out for a gamma ray photon in the sun. First, conditions are not the same all throughout the sun, so the distance between gamma ray "crashes" with other particles changes. Second, no one has ever visited the center of the sun, so some assumptions need to be made, anyway. There are all sorts of reasonable assumptions, varying from one-tenth of a millimeter to about a centimeter. The choice of this distance has a big impact on the time calculation.
How Long it Takes
The radius of the sun is 700,000 kilometers, which is 7 trillion "steps" if each step is a tenth of a millimeter, and 70 billion steps if each step is 1 centimeter. From the drunkard's-walk problem, you know that the average number of steps it takes to get a certain distance is equal to the square of the number of steps it would take to go in a straight line. So it would take 49 trillion trillion steps of 0.1 millimeter and 490 billion trillion steps of 1 centimeter each. The time it takes to travel those steps is the total distance divided by the speed of light. So, if you think photons only travel 0.1 millimeters between crashes, it will take more than half a million years for the photon to escape the sun. If you think it's about a centimeter, then it will take about 5,000 years for the photon to get outside the sun.