If you need to generate random numbers in a game or a simulation, or you’re writing tests and need test data, there are a lot of ways to do it. Some of them are very simple, others involve advanced math. This tutorial will explain some ways to do it with a theoretical focus on speed and precision.

There are many different ways to generate random data. This tutorial will focus on the fastest and most precise methods of generating truly random numbers, and show you how to implement those methods in any language.

You can generate random numbers with a variety of precision levels. We’ll start with low-precision pseudo-random numbers, move onto medium-precision floating point random numbers, and finally look at high-precision floating point random numbers.

When it comes to random data, there are two types of applications that come to mind: those that need a very high level of precision, and those where speed is paramount. If you’re generating random numbers in a scientific application, or working with cryptography, then you likely need the highest possible precision. Otherwise, you just want something quick and easy to use. What follows is a tutorial on generating random numbers using JavaScript.

Speed

If you want speed, you can use Math.random(). This method returns a number between 0 (inclusive) and 1 (exclusive):

Math.random()

0.7358410674759165

Math.random()

0.7511572548621386

Math.random()

0.2701887435018541

This function is so common that all modern browsers implement it in native code for speed reasons, so there’s no need for a polyfill or anything like that—just include it in your site and use it! However, the results from Math.random() will have much less precision than you might expect: six decimal places at most! In some cases this will be more than enough precision, but if you need more there are other options available to you.

Writing a random number generator is surprisingly hard. Much of the difficulty is in ensuring that it will run quickly and generate high-quality random numbers on a wide range of hardware.

Most existing random number generators are designed for simplicity, not speed or quality. They are generally designed to be cryptographically secure (to prevent an adversary from predicting future output based on past output), but the cost is low performance. Conversely, some generators sacrifice quality for speed. The xorshift128+ generator described in this post offers both speed and security.

This tutorial demonstrates how to implement xorshift128+ and describes how it works. We’ll also look at how to test whether a random number generator is working well.

The first step is to generate random numbers. If you want to create an array of random numbers, there is a very simple function just for that, aptly named np.random.rand()

Let’s say we want to generate an array of 20 random numbers. We can do this by using the following code snippet:

>>> import numpy as np

>>> x = np.random.rand(20)

The output of the function will be 20 random numbers between 0 and 1:

array([ 0.35208064, 0.67367532, 0.91203407, 0.3729784 , 0.23555615, 0.18339952, 0.93097325, 0.77683956, 0.09525342, 0.39083862,0.57304783, 0.72474114, 0.50447466, 0.53749067, 0.09724522,0.63327242, 0.63125816, 0.43695911, 0.70325718])

The following JavaScript code uses the Math.random() function to generate a random number between 0 and 1, then multiplies that number with the maximum value we want to get, then rounds it up or down with Math.floor() or Math.ceil().

shuffle(

array,

beginIndex = 0,

endIndex = array.length

)

Shuffle the elements in a given array in place. The begin/end indices indicate which elements to include in the shuffle. The length of the array will remain the same after shuffling.