How to get a random number that follows a particular distribution?

For many applications, I need a gaussian random variable, or a random variable which follows a non-gaussian distribution (e.g. a t-distribution).

Now, the rand() command allows you to get a random int, but how can this be used to generate a random float which is sampled from a particular distribution? The rand() command gives you each value with equal probability -- here I want something quite different.

Now, I know I can, by a trick, get a normal distribution, by using the CLT, like this:

Code:

`int i;`

double randomNormal = 0;

for (i = 0; i < 50; i++){

randomNormal += ((double)1/(double)rand());

}

The CLT, BTW, states that if you average over many points from any probablity distribution (here, 1/rand() is that distro), the average itself will always follow a normal distribution.

But, this, although it gets me a gaussian distribution, does not help for other types of probablity distributions. Is there a general way to generate samples from an arbitrary distribution given a formula for its PDF?