Inspiration
People have been using random numbers for millennia, so the concept isn't new.
Randomness has many uses in science, statistics, cryptography and more.
Many programming languages contain classes that allow for the functionality of a random number generator,
but here I will create my own method that does this.
How it works
This method of generating random numbers involves the computational algorithms that can produce random results.
The set of instructions to be followed in my method is the following:
- Accept some initial input number, that is a seed or key.
- Apply that seed in a sequence of mathematical operations to generate the result. That result is the random number.
- Use that resulting random number as the seed for the next iteration.
- Repeat the process to emulate randomness.
This generator produces a series of random numbers.
Given an initial seed X0 and integer parameters a as the multiplier, b as the increment, and m as the modulus, the generator is defined by the linear relation: Xn ≡ (aXn-1 + b)mod m.
Or using more programming friendly syntax: Xn = (a * Xn-1 + b) % m.
Each of these members have to satisfy the following conditions:
- m > 0 (the modulus is positive)
- 0 < a < m (the multiplier is positive but less than the modulus)
- 0 ≤ b < m (the increment is non negative but less than the modulus)
- 0 ≤ X0 < m (the seed is non negative but less than the modulus)
How we built it
Let's create a JavaScript function that takes the initial values as arguments and returns an array of random numbers of a given length:
// x0=seed; a=multiplier; b=increment; m=modulus; n=desired array length;
const linearRandomGenerator = (x0, a, b, m, n) => {
const results = []
for (let i = 0; i < n; i++) {
x0 = (a * x0 + b) % m
results.push(x0)
}
return results
}
What we learned
This algorithm is called Linear Congruential Generator and it is one of the oldest and best-known algorithms. As for random number generator algorithms that are executable by computers, they date back as early as the 1940s and 50s and continue to be written today.
Log in or sign up for Devpost to join the conversation.