how to understand the generation of cauchy distribution from uniform distribution? I am learning some basic idea on generating cauchy distribution from uniform random generator $u \in [0, 1]$. I know it was discussed before in How to generate a Cauchy random variable, but during my realization in matlab, I still don't understand how to interpret the results. Here is my matlab code
gam = 10; 
x0 = 0;
u=rand(1,1000);
r = x0 + gam.*tan(pi*(u-0.5));
hist(r);

x = -1000:0.01:1000;
y = (1/pi)*(gam./(gam^2+(x-x0).^2));
plot(x, y); 

I got the result from the hist(r) as 

However, in second part of code where I plot the actual Cauchy distribution, I saw a very different shape as the first figure. Am I missing anything here?

 A: First, as others have mentioned, you're using the default settings for the hist function. As stated right at the beginning of the documentation, this uses 10 equally-spaced bins by default. Try increasing the number:
gam = 10; 
x0 = 0;
u = rand(1,1e4);
r = x0 + gam.*tan(pi*(u-0.5));
hist(r,1e3);
axis([-2000 2000 0 1e4]);

The vertical (dependent) axis is in terms of bin counts as this is a histogram, not a probability density function (PDF) plot. However, just being able to specify the number of bins is not very flexible. This one of several reasons why The MathWorks discourages the use of hist and the underlying histc in current versions of Matlab.
A better, more-flexible alternative is the histogram function, which does a better job of automatic binning even without specifying any additional settings. Additionally, it has options for easily normalizing the histogram as if it were a PDF, if that's what you're after
gam = 10; 
x0 = 0;
u = rand(1,1e4);
r = x0 + gam.*tan(pi*(u-0.5));
edges = -200:10:200;
histogram(r,edges,'Normalization','pdf');
hold on;
x = -200:0.01:200;
y = (1/pi)*(gam./(gam^2+(x-x0).^2));
plot(x,y);
axis([x(1) x(end) 0 1.1*max(y)]);

This produces a plot like this:


Lastly, you can use kernel density estimation via Matlab's ksdensity function:
gam = 10; 
x0 = 0;
u = rand(1,1e3);
r = x0 + gam.*tan(pi*(u-0.5));
x = -200:0.01:200;
y = (1/pi)*(gam./(gam^2+(x-x0).^2));
f = ksdensity(r,x);
plot(x,y,'r--',x,f,'b');
axis([x(1) x(end) 0 1.1*max(y)]);

which yields a plot something like this:

More random samples will result in smoother estimates that match your PDF more closely. Be sure to read through the documentation for ksdensity. 
A: Try to limit the range of your histogram. There are a small number of very extreme values in your Cauchy random sample that are causing the histogram to extend over a very large range, and at the same time causing almost all values to occupy one bin. Notice the density plot ranges only from -1000 to 1000. If you impose a similar range on your histogram (even better, make both plots run from -200 to 200), you won't have almost all your values living in one histogram bin, and the shape of the histogram will become more apparent.
