Two ways: a pseudo random number generator (which is predictable from a given seed point) or a number generated from a generally unknown but measurable value, e.g. CPU temperature at any given point in time. Both are not truly random in the general sense, but are random ‘enough’ for day to day applications.
>is there a formula for a random number?
There are many.
These formulas are known as Pseudorandom Number Generators(PRNG).
They always take in a number called a “seed” and then do math at it till it spits out seemingly random numbers.
Though a side effect of this is that if you use the same formula on the same seed, you get the same stream of seemingly random numbers.
They vary on how close to random it is, how hard it is to guess the seed given some example of the stream, and how hard it is for a computer to run.
Sometimes though they need real random numbers.
So to do that usually they need a piece of hardware that measures something physical to make up random number generators.
Random.org has some radio stuff to see what kinda random noise is coming from the atmosphere.
Cloudflare has a camera pointed to a wall of lava lamps and makes numbers from the photos.
Others use quantum mechanics stuff and other weird things.
Often these numbers will be fed into PRNG as the seed to make it even more random incase the physics stuff is somehow a little more predictable that moment.
Two ways: a pseudo random number generator (which is predictable from a given seed point) or a number generated from a generally unknown but measurable value, e.g. CPU temperature at any given point in time. Both are not truly random in the general sense, but are random ‘enough’ for day to day applications.
There’s no formula for random numbers, as formulas will output the same result for the same input. Computers are deterministic, but random numbers require true randomness, which a function will never provide you with.
So you either use real random number generators that for example listen to cosmic background radiation, but they are slow and more expensive. If security is of high importance something like with will get used.
Or you can use pseudo random number generators. A simple one is a function like Y = (a * X + b) mod c where X is either the seed (for example the current time in milliseconds) or the last result. They will get used in applications where security isn’t as important but speed is, for in video games.
Let’s use an easy example (in the real world we would use larger and better numbers): a = 7, b = 3, c = 5
X = 0 -> 3 mod 5 = 3
X = 3 -> (7*3 + 3) mod 5 = 4
X = 4 -> (7*4 + 3) mod 5 = 1
X = 1 -> (7 + 3) mod 5 = 0
In this example we picked bad numbers as it would just cycle between 3, 4, 1 and 0 without ever producing a 2. You want to set a, b and c to values that result in every possible result coming out with the same frequency and in an order that appears to be random.
As you see it would always produce the same cycle of numbers so a pseudo random number generator will never be completely secure. If you initialize it with for example the current time an attacker could just try to pinpoint that time and end up with the same set of results as your application.
There’s no formula for random numbers, as formulas will output the same result for the same input. Computers are deterministic, but random numbers require true randomness, which a function will never provide you with.
So you either use real random number generators that for example listen to cosmic background radiation, but they are slow and more expensive. If security is of high importance something like with will get used.
Or you can use pseudo random number generators. A simple one is a function like Y = (a * X + b) mod c where X is either the seed (for example the current time in milliseconds) or the last result. They will get used in applications where security isn’t as important but speed is, for in video games.
Let’s use an easy example (in the real world we would use larger and better numbers): a = 7, b = 3, c = 5
X = 0 -> 3 mod 5 = 3
X = 3 -> (7*3 + 3) mod 5 = 4
X = 4 -> (7*4 + 3) mod 5 = 1
X = 1 -> (7 + 3) mod 5 = 0
In this example we picked bad numbers as it would just cycle between 3, 4, 1 and 0 without ever producing a 2. You want to set a, b and c to values that result in every possible result coming out with the same frequency and in an order that appears to be random.
As you see it would always produce the same cycle of numbers so a pseudo random number generator will never be completely secure. If you initialize it with for example the current time an attacker could just try to pinpoint that time and end up with the same set of results as your application.
Latest Answers