How do random number generator works? (for example in C/C++ Java)
How can I write my own random number generator? (for example in C/C++ Java)
There is also this algorithm:

Oh, and more seriously:
Random number generators use mathematical formulas that transfer set of numbers to another one. If, for example, you take a constant number N and another number n_0, and then take the value of n mod N (the modulo operator), you will get a new number n_1, which looks as it if is unrelated to n_0. Now, repeat the same process with n_1 and you'll get another number. What you have here is a (VERY BAD) generator of seemingly random numbers.
Remember, the method I've described here is a toy method that should not be used for anything serious. It does, however, illustrate the general principle.
Also note:
If all scientific papers whose results are in doubt because of bad
rands were to disappear from library shelves, there would be a gap on each shelf about as big as your fist.
(paraphrased from chapter 7 of Numerical recipes). This is a must-read text for anyone who uses random number generators for any serious work.