Question: I want to generate random numbers manually. I know that every language have the rand or random function, but I'm curious to know how this is working.
Does anyone have code for that?

Answer: 
POSIX.1-2001 gives the following example of an implementation of `rand()` and `srand()`, possibly useful when one needs the same sequence on two different machines.

```
static unsigned long next = 1;
/* RAND_MAX assumed to be 32767 */
int myrand(void) {
    next = next * 1103515245 + 12345;
    return((unsigned)(next/65536) % 32768);
}
void mysrand(unsigned seed) {
    next = seed;
}

```
Most RNGs(random number generators) will require a small bit of initialization. This is usually to perform a seeding operation and store the results of the seeded values for later use. Here is an example of a seeding method from a randomizer I wrote for a game engine:

```
    /// <summary>
    /// Initializes the number array from a seed provided by <paramref name="seed">seed</paramref>.
    /// </summary>
    /// <param name="seed">Unsigned integer value used to seed the number array.</param>
    private void Initialize(uint seed)
    {
        this.randBuf[0] = seed;
        for (uint i = 1; i < 100; i++)
        {
            this.randBuf[i] = (uint)(this.randBuf[i - 1] >> 1) + i;
        }
    }

```

This is called from the constructor of the randomizing class. Now the real random numbers can be rolled/calculated using the aforementioned seeded values. This is usually where the actual randomizing algorithm is applied. Here is another example:

```
    /// <summary>
    /// Refreshes the list of values in the random number array.
    /// </summary>
    private void Roll()
    {
        for (uint i = 0; i < 99; i++)
        {
            uint y = this.randBuf[i + 1] * 3794U;
            this.randBuf[i] = (((y >> 10) + this.randBuf[i]) ^ this.randBuf[(i + 399) % 100]) + i;
            if ((this.randBuf[i] % 2) == 1)
            {
                this.randBuf[i] = (this.randBuf[i + 1] << 21) ^ (this.randBuf[i + 1] * (this.randBuf[i + 1] & 30));
            }
        }
    }

```

Now the rolled values are stored for later use in this example, but those numbers can also be calculated on the fly. The upside to precalculating is a slight performance increase. Depending on the algorithm used, the rolled values could be directly returned or go through some last minute calculations when requested by the code. Here is an example that takes from the prerolled values and spits out a very good looking pseudo random number:

```
    /// <summary>
    /// Retrieves a value from the random number array.
    /// </summary>
    /// <returns>A randomly generated unsigned integer</returns>
    private uint Random()
    {
        if (this.index == 0)
        {
            this.Roll();
        }

        uint y = this.randBuf[this.index];
        y = y ^ (y >> 11);
        y = y ^ ((y << 7) + 3794);
        y = y ^ ((y << 15) + 815);
        y = y ^ (y >> 18);
        this.index = (this.index + 1) % 100;
        return y;
    }

```