There is a whole field of research on it
https://en.wikipedia.org/wiki/Random_number_generation
In short, there are two types:
Pseudo-random number generation
This is the type we normally use in computing. The numbers are not really random: they follow a very long sequence that is repeatable, if you start from the same state.
However if you can somehow scramble the starting state to a truly random value, then the sequence of numbers meets most requirements of randomness for games etc, but not for cryptography.
True random number generation
This relies on getting information into the computer that is not predictable or repeating. This prevents the repeating pattern from being used by an attacker to subvert a cryptography scheme.
Typically we get this source randomness from physical sensors on the computer, e.g.
- timing of mouse clicks and keypresses (which are unlikely to be repeated)
- fine detail of values of temperature sensors inside the computer
- other sensor information that contains noise
Sometimes these input sources are called "entropy". With a modest amount of entropy from true random number generation you can "seed" a pseudo-random number generator, well enough for normal non-cryptographic purposes.