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 a lot of information available about how they are working ... see Konamiman's answer and use google a bit.
Why would you like to write a new random generator? You probably should not try to do so ... until you need something very special. For example in a game you could use a shuffle bag which produces 'fair' random values - have a look at this interesting question on SO.
I post this here, because I really liked the idea and implementation when I read about it the first time :)