While solving programming problems we want to add some randomness to the solutions we design. This can be the case if you are building an application for Casino or some other online lucky draw app, then you surely want things to get 'random'.
To enable this functionality, random numbers come into the picture. As we know that computers are good at maths and thus they can easily generate random numbers.
Here, Math is the name of the class and random is the method name.
When you call this method it will give a pseudo-random number between 0 and 1.
NOTE : In the output 0 is inclusive and 1 is exclusive. This means the output can have 0 but it will not have 1.
The output of this code is not fixed, of course, you generating a random number you do not want it to be the same.
An interesting fact about random numbers: The computers we use are finite state machines which means that their outcomes are deterministic and theoretically we should not be able to generate random numbers.
But I have seen an app that gives me random numbers every time! How is this possible then? You are not wrong, but also not 100% right. Let me explain.
Numbers that our computers generate are called pseudo-random numbers. While these numbers CAN repeat, that doesn't mean they do so consistently. Pseudo-random numbers are good enough for our practical applications. The algorithms that generate these numbers make them very hard to repeat so we do not need to worry about the random number is "not random".
The algorithms do a good job of simulating randomness. This is the reason why they are technically called Pseudo-random number generators (PRNGs).
When we use math to create a sequence of numbers, these numbers may initially appear random, but gradually they will repeat and reveal a pattern that is not random.
The PRNG is an algorithm and one PRNG can be better than the other. These PRNG use different factors to generate numbers one of the factors being period which is how many iterations a PRNG undergoes before it begins to repeat itself.
One famous algorithm is xorshift128+. To give some idea of how this algorithm works, it uses 3 bitwise operations:
- Left Shift
- Right Shift
Left Shift (<<)
Left shift is a bitwise operation that shifts all the bits to the left.
Right Shift (>>)
Right shift is a bitwise operation that shifts all the bits to the right.
XOR stands for Exclusive OR and this operation compares the binary representation of two numbers. The output is 1 if both the bits are different and 0 if both are the same.
xorshift128+ uses these operations to evaluate the expression similar to this s1 ^= s1 << 23. This is a very oversimplified version but this gives an idea of how the algorithm works.
Generate Random Numbers Between Two Numbers
To generate random numbers in a range of numbers we can multiply the output of the Math.random() method by the difference between those numbers and then we add the smaller number.
Generate a Random Integer Between Two Numbers
If we want to generate a random integer between two given numbers then we can use the code used above and can use Math.floor() on the result we get from Math.random().
Generate Integer between Two Numbers(inclusive)
To generate random numbers between two numbers and including both the min and the max number we can code
|Supported||Version (Full Support)|
After reading this article you have learnt
- Random numbers are not really random, they are pseudo-random.
- A little idea of the popular algorithm xorshift128+
- Bitwise operations: Left Shift, Right Shift, and XOR