Generating random numbers from a t-distribution How can I generate random numbers that follow a student-t distribution?
From several sources I understand that this can be done using a random sample of size $n$ drawn from a normally distributed population, as follows:
$$
t = \frac {(x - m)} {(s/\sqrt n)}
$$
Where $x$ is the sample mean, $m$ is the mean of the normal distribution (I assume you can just use the standard normal distribution, so $m=0$?), and $s$ is the sample standard deviation.
The degrees of freedom of the student-t distribution will then be $n-1$.
Do I understand correctly that in order to generate a random student-t value with $f$ degrees of freedom, I should first generate $f+1$ normally distributed values (i.e. standard normal), and then calculate the mean ($x$) and standard deviation ($s$) of these and apply the above formula?
And if I do this repeatedly many times, the resulting random values will approach a student-t distribution with $f$ degrees of freedom?
I tried this in Excel using a macro that uses the above formula and another macro that generates random Gaussians (which works, I tested it) but the resulting random values do not seem to be completely student-t distributed. For instance with 6 degrees of freedom, the variance of 10,000 random values is about $1.7$ while it should be $6/(6-2) = 1.5$.
 A: Given a generator of i.i.d. standard gaussian random variates, you can generate $t_k$ distributed random variates (with any positive integer degree of freedom $k$) by using the relation:
$$Y=\frac{X_{k+1}}{\sqrt{k^{-1}\sum_{i=1}^k X_i^2}}$$ 
where $Y\sim t_k$  and $X_i\sim\text{i.i.d. }\mathcal{N}(0,1),i=1,\ldots,k+1.$
A: I have an answer to the practical part of your question, though not quite the theoretical one.
There is a function called TINV that directly does this. Except that it conly returns positive random t variates. You can get around that limitation with the following formula:
=TINV(RAND(),6)*(RANDBETWEEN(0,1)*2-1)

...you can replace 6 with whatever value you want for the DF, and the rand() can be replaced with any number between 0 and 1. The rest of it simply guarantees equal probability of negative and positive values.
A: A fast way of generating a t variate, faster than the gaussian-only approach for all but the smallest degrees of freedom, is to use the fact that a t distribution is a mixture of Normals, with the mixture distribution being an inverted gamma distribution on the variance.  Here's an example in R, where we generate 1,000,000 t(10) variates this way and compare to the theoretical distribution using the Kolmogorov-Smirnov test ("proof" by large experiment!):
> df <- 10
> s2 <- 1/rgamma(1000000, df/2, df/2)
> tv <- rnorm(1000000,0,sqrt(s2))
> 
> ks.test(tv, pt, df=df)

    One-sample Kolmogorov-Smirnov test

data:  tv 
D = 6e-04, p-value = 0.8826
alternative hypothesis: two-sided 

This approach also works for non-integer degrees of freedom, which the gaussian-only approach does not.
