Random Number Generators

Random Number Generator: How Do Computers Generate Random Numbers?

People have been playing random numbersfor millennia. So this idea isn't a new one. From the lottery that was played in the ancient city of Babylon to Roulette tables at Monte Carlo, to dice games in Vegas, the goal was to make the end result to random luck.

In addition to gambling, randomnesshas many applications for science, statistics cryptographyand many more. However, using coins, dice or other similar media as a random device comes with its limitations.

Because of their mechanical character of methods, generatinglarge quantities of random numbers requires great amount of time and effort. Thanks to the human brain, we are able to use more effective tools and methods at our disposal.

Methods of generating random numbers

True Random Numbers

Picture of analog-input digital-output processing device. Photo by Harrison Broadbent

Let's look at two main techniques used to generate random numbers. The first methodis the HTML1 method, which isbased on physical processes, and takes the randomness source from a nature phenomenon believed to have random.

Such a phenomenon takes place beyond the computer. It is recorded and adjusted to take into account possible biases due to measurements. Examples include photoelectric effect cosmic background radiation atmospheric noise (which we will discuss to illustrate this story) and many more.

Thus, random numbers generated using this type of randomness are believed to be " true" random numbers.

The technical aspect comprises a component that converts energy from one form to another (for instance, radiation converts to one that is electrical) as well as an amplifier and an analog-to-digital conversion device to convert the output to a digital number.

What are Pseudorandom Numbers?

Picture of computer code flowing through computer screen. Photo by Markus Spiske.

In addition in lieu of "true" random numbers, the second way for generating random numbers involves computational algorithms that could produce seemingly random results.

Why does it seem so random? Because the end results obtained are in fact completely dependent on an initial value called"the key value . It is also known as the key. If you were aware of the value of the key, and also how the algorithm functions, you could reproduce these almost random results.

Random number generators like this are typically referred to Pseudorandom number generators and, as consequence, they generate pseudodorandom Numbers.

Although this type of generator typically doesn't gather any data from sources of naturally occurring randomness, such gathering of keys can be made feasible if needed.

Let's take a look at the similarities and differences between TRNGs, or true random number generators. TRNGs and pseudorandom number generators also known as PRNGs.

PRNGs run faster than TRNGs. Due to their deterministic nature they can be beneficial when you need to replay an event that is random. This is extremely helpful in testing code for instance.

On the other hand TRNGs do not have a regular schedule and are more effective in the security-sensitive areas like encryption.

An duration is the number of times a PRNG cycle through before it repeats itself. So, all else being equal, a PRNG running more time would require more computer resources to determine and even crack.

Example Algorithm for Pseudo-Random Number Generator

A computer runs code that is in accordance with a set rules that must be adhered to. For all PRNGs the rules are the following:

  1. Accept an initial input number, which is a key or seed.
  2. Apply that seed in a sequence of mathematical operations that produce the end result. This results in the random number.
  3. Use that random numbers as your seed number for the next repetition.
  4. It is possible to repeat the process until you achieve randomness.

Let's now look at an illustration.

The Linear Congruential Generator

This generator produces a series of pseudorandom numbers. With an initial seed of X0 with integer parameters a as the multiplier, B as the increment and m as modulus, the generator can be described using the linear relation"Xn" (aXn-1 + b)mod the modulus m. In a simpler programming formula: X n = (a * X n-1 + b) percent the value of.

Each of the members has to meet the following requirements:

  • m > 0.(the module is positive),
  • 0 , a, M(the multiplication factor is positive but smaller than the modulus),
  • 0.= b m (the increment is not negative, however it is less in comparison to the modulus), and
  • 0.<(= the value of X 0 < the m(the seed is not negative but less than its modulus).

Let's design an JavaScript function that accepts the values that were given as initial arguments to return an array of numbers with 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  

The Linear Consgruential Generator one of the oldest and most well-known PRNG algorithms.

In the case of random number generator algorithms that are able to be executed by computers, they were written in the 1950s and 1940s (the Middle-square method and the Lehmer generator, for example) and continue to be developed today ( Xoroshiro128+, Squares RNG and many others).

A Sample Random Number Generator

When I decided to write this piece about embedding an automated random number generator inside a web page, I was faced with a decision.

I could've utilized JavaScript's Math.random()function as the base and generated results in pseudorandom amounts, as I have done in previous articles (see Multiplication Chart Code Your Own Times Table).

However, this post revolves around generating random numbers. So I decided to learn how to gather "true" randomness based data and share what I learned with you.

The following will be the "true" Random Number Generator. Enter the parameters, then hit Generate.True Random Number GeneratorBinary Decimal Hexadecimal GenerateResult:

The code fetches data from some API via Random.org. This online resource has a plethora of useful instruments that are flexible and customizable, and comes with a great documentation with it.

The randomness comes from atmospheric noise. I was able to use asynchronous functions. This is an enormous benefit going forward. The function at its core is this:

// Generates a random number within user indicated interval const getRandom = async (min, max, base) =>   const response = await  fetch("https://www.random.org/integers/?num=1&min="+min+"  &max="+max+"&col=1&base="+base+"&format=plain&rnd=new")  return response.text()   

The parameters it accepts permit users to personalize the output of random numbers. For example, min and max allow users to set lower and higher limit on output generated. Furthermore, base determines if output is printed as binary, decimal or hexadecimal.

Again, I chose this one, however there are many other options available at the source.

After you click the Generate button after which that handleGenerate() function is called. It , in turn, invokes the getRandom() asynchronous function to handle error handling and then outputs results:

// Output handling const handleGenerate = () => in

The rest of the code deals with HTML design, structure and styling.

The program is ready to be used and embedded in this website page. I broke it down into component parts and included detailed comments. It is easily modified. You can also modify the functionality and styles as your needs require.

Comments

Popular posts from this blog

What Is a Calorie?

Why are BMIs useful ?