Random Number Generator

Random Number Generator: How Do Computers Generate Random Numbers?

People have been using random numbers for millennia. Therefore, the concept isn't entirely new. From the lottery in ancient Babylon, to roulette machines in Monte Carlo, to dice games in Vegas the aim of the game is to let the final result to random chance.

However, aside from gambling, randomnesshas many applications in the fields of science, statistics, cryptography and more. However, using coins, dice or other similar media as an random device comes with its own limitations.

Due to its mechanical basis for these techniques, generating large quantities of random numbers will require a plenty of time and effort. Thanks to human ingenuity, we have more efficient techniques and tools that are available.

Methods for generating random numbers

True Random Numbers

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

Let's consider two principal methods to generate random quantities. The first one is built on a physical process and extracts the cause of randomness in a physical phenomenon which is believed as random.

This type of event occurs outside of the computer. It is measured and then adjusted to correct for any distortions caused by measuring processes. Examples include radioactive decay The photoelectric effect, cosmic background radiation, atmospheric noise (which we will employ within this post), and more.

So, random numbers that are generated from such randomness are said to be " true" random numbers.

The hardware consists of a device that transforms energy to another (for example, radiation , to electricity), an amplifier, and an analog-to-digital conversion device to turn the output into a digital number.

What are Pseudorandom Numbers?

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

In addition for "true" random numbers, the second method of creating random numbers involves using algorithms that may produce random results.

Why do we think that it is random? Since the final results are actually dependent on an initial number called"the "seed" value , or the key. Thus, if you know the value of the key and how the algorithm functions you could duplicate these almost random results.

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

Although this kind of generator typically doesn't gather any information from natural randomness, gathering keys is possible at times when it is needed.

Let's review some similarities between the real random number generators, or TRNGs and pseudorandom number generators also known as PRNGs.

PRNGs are quicker than TRNGs. Due to their inherent deterministic nature, they're efficient when you need to replay a series of random events. This can be very helpful in testing code for example.

However TRNGs do not have a regular schedule and perform better in areas that require security, such as encryption.

In the context of PRNGs, a length is the amount of times a PRNG cycle through before it repeats itself. All other things being the same, a PRNG that has an extended period will require more computer resources in order to predict and even crack.

Example Algorithm for Pseudo-Random Number Generator

Computers execute code that is in accordance with a set rules to be followed. For PRNGs in general the rules are the following:

  1. Accept some initial input number, which is a key or seed.
  2. Apply the seed to a series of mathematical operations to generate the result. The result is a random number.
  3. Use the resulting random numbers as the basis for your next version.
  4. Then repeat the process to emulate randomness.

Let's take a take a look at an example.

The Linear Congruential Generator

This generator generates a string of pseudorandom numbers. In the case of an initial seed, X0 and integer parameters such as a as the multiplier, an increment as b, and m as the modulus, it is defined as the linear relation: Xn (aXn-1 + b)mod M. Or using more programming friendly formula: X n = (a * X n-1 + b) % M.

Each of these members have to meet the following criteria:

  • m > 0.(the module is positive),
  • 1 a (the multiplier is positive, but less than the modulus),(the multiplier of the multiplier, which is positive, but smaller than the modulus),
  • 0= b < (the increment is not negative but less than the modulus) (the increment is non negative but less than the modulus) and
  • 0<= X 0 < M(the seed isn't negative, but is less than the modulus).

Let's design the JavaScript function that takes the arguments as the starting values to return an array of random numbers of a given length:

The Linear Congruential Generator (LCG) is among of the oldest and best-known PRNG algorithms.

When it comes to random number generator algorithms that can be executed by computers they have been in use since to the 1950s and 1940s (the Middle-square method and Lehmer generator for instance) and are still being written today ( Xoroshiro128+, Squares RNG, and many more).

A Sample Random Number Generator

When I was deciding to write this article about embedding a random number generator on an internet page I was faced with a difficult decision to make.

I could've made use of JavaScript's Math.random()function to serve as the basis and generate results in pseudorandom numbers as I have in earlier articles (see Multiplication Chart code your own Times Table).

However, this post is about the process of creating random numbers. So I decided to find out how to gather "true" randomness based data and share it with you.

Here is the "true" Random Number Generator. Set the parameters and hit Generate.True Random Number Generator Binary Decimal Hexadecimal GenerateResult:

The code retrieves data from an API, courtesy of Random.org. The site has a plethora of useful tools, which can be easily customized and comes with excellent documentation that goes with it.

The randomness stems from atmospheric noise. I was able to use Asynchronous functions. This is a huge benefit in the future. The basic function of the system is this:

The parameters it uses allow users to personalize random numbers output. For example, min and max allow you to set upper and lower limits on generated output. Furthermore, base decides if the output is printed in binary decimal, decimal or hexadecimal.

Again, I chose this configuration but there are many more available at the source.

When you click the Generate button After you click it, when you click the Generate button, the handleGenerate() function is called. It calls the getRandom() asynchronous function and handles error handling and outputs results

The remainder of the code is concerned to HTML structure, appearance, and styling.

The program is waiting to be used and embedded on this page. I have broken it up into smaller parts and provided specific notes. It is easy to modify. You can alter the functionality and style as your needs require.

er Arobelidze

The fascination with the field of Mathematics provides a great service in my pursuit to become a successful developer. I am excited by the thought of helping others find high-quality resources.

The code-learning course is free. freeCodeCamp's opensource course has helped over 10,000 people to find work as software developers

Comments

Popular posts from this blog

Calorie Calculator

power-converter