# Random numbers are not Random!

T he truth is that no body tolds you about is random numbers are not really random they can be predicted.The random number classes or libraries that we use in our code are not True Random Number instead they are Pseudo-Random Number.They behaves like they are random but thet aren’t.As the word ‘pseudo’ suggests, pseudo-random numbers are not random in the way you might expect, at least not if you’re used to dice rolls or lottery tickets. Essentially, PRNGs( Pseudo-Random Number Generators) are algorithms that use mathematical formula or simply precalculated tables to produce sequences of numbers that appear random. A good example of a PRNG is the linear congruential method. A good deal of research has gone into pseudo-random number theory, and modern algorithms for generating pseudo-random numbers are so good that the numbers look exactly like they were really random but they aren’t.Its like saying HTML is a programming language.

# How Pseudo-Random Number Generators Work

Suppose we want to generate a random number between 1 and 52, where every number has an equal probability of appearing. Ideally, we would generate a value on the range from 0 to 1 where every value will occur with equal probability, regardless of the previous value, then multiply that value by 52. Note that there are an infinite number of values between 0 and 1. Also note that computers do not offer infinite precision!

In order to program a computer to do something like the algorithm presented above, a pseudo-random number generator typically produces an integer on the range from 0 to N and returns that number divided by N. The resulting number is always between 0 and 1. Subsequent calls to the generator take the integer result from the first run and pass it through a function to produce a new integer between 0 and N, then return the new integer divided by N. This means the number of unique values returned by any pseudo-random number generator is limited by number of integers between 0 and N. In most common random number generators, N is 2³² (approximately 4 billion) which is the largest value that will fit into a 32-bit number. Put another way, there are at most 4 billion possible values produced by this sort of number generator. To tip our hand a bit, this 4 billion number is not all that large.

A number known as the seed is provided to a pseudo-random generator as an initial integer to pass through the function. The seed is used to get the ball rolling. Notice that there is nothing unpredictable about the output of a pseudo-random generator. Each value returned by a pseudo-random number generator is completely determined by the previous value it returned (and ultimately, the seed that started it all). If we know the integer used to compute any one value then we know every subsequent value returned from the generator.

The pseudo-random number generator distributed with Borland compilers makes a good example and is reproduced in Figure 1. If we know that the current value of RandSeed is 12345, then the next integer produced will be 1655067934 and the value returned will be 20. The same thing happens every time (which should not be surprising to anyone since computers are completely deterministic).

## Figure 1: Borland’s implementation of Random()

long long RandSeed = #### ;unsigned long Random(long max)

{

long long x ;

double i ;

unsigned long final ;

x = 0xffffffff;

x += 1 ; RandSeed *= ((long long)134775813);

RandSeed += 1 ;

RandSeed = RandSeed % x ;

i = ((double)RandSeed) / (double)0xffffffff ;

final = (long) (max * i) ; return (unsigned long)final;

}

Based on historical precedent, seeds for number generators are usually produced based on the system clock. The idea is to use some aspect of system time as the seed. This implies if you can figure out what time a generator is seeded, you will know every value produced by the generator (including what order numbers will appear in). The upshot of all this is that there is nothing unpredictable about pseudo-random numbers. Needless to say, this fact has a profound impact on shuffling algorithms!

**How to Generate True Random Numbers?**

RANDOM.ORG is a true random number service that generates randomness via atmospheric noise.You can imagine this as a die connected to a computer, but typically people use a physical phenomenon that is easier to connect to a computer than a die is. The physical phenomenon can be very simple, like the little variations in somebody’s mouse movements or in the amount of time between keystrokes. In practice, however, you have to be careful about which source you choose. For example, it can be tricky to use keystrokes in this fashion, because keystrokes are often buffered by the computer’s operating system, meaning that several keystrokes are collected before they are sent to the program waiting for them. To a program waiting for the keystrokes, it will seem as though the keys were pressed almost simultaneously, and there may not be a lot of randomness there after all.

So,now you have a good understanding on how random numbers and generated and how they can be predicted.Always try to make things more random because Random’s are not Random.

✉️

Subscribe toCodeBurst’sonce-weeklyEmail Blast🐦,FollowCodeBurston, view🗺️The 2018 Web Developer Roadmap, and🕸️Learn Full Stack Web Development.