# Random Numbers in Matlab – Part II

This is our second post in our series on random numbers in Matlab. The first post can be found here. In this post, I will explain how to control the random number generation functions in Matlab and discuss alternatives for projects with stronger requirements for randomness, such as cryptography.

## Random number generation

Random number generation in Matlab is controlled by the rng function. This function allows the user to specify the seed and generation method used in random number generation as well as save the current settings so that past experiments can be repeated. By default, rng starts with a seed of zero and uses the Mersenne Twister generation method. Whenever Matlab restarts, the seed of rng is reset to zero, which means that the same random numbers will be generated in the same order every time Matlab is restarted. If avoiding this is desireable, use the ‘shuffle’ seed.

```>> t = rng      %Stores current settings of rng in t
t =

Type: 'twister'
Seed: 0
State: [625x1 uint32]

>> rand(1,4)     %Generates a 1x4 random matrix with default initial
%state
ans =

0.8147    0.9058    0.1270    0.9134

>> rand(1,4)     %Generates a 1x4 random matrix
%(different from initial random matrix)
ans =

0.6324    0.0975    0.2785    0.5469

>> rng(t)        %Restore settings of rng
>> rand(1,4)     %New random matrix same as initial random matrix
ans =

0.8147    0.9058    0.1270    0.9134
>> rng('shuffle')     %Seed rng on basis of current time
>> rng
ans =

Type: 'twister'
Seed: 385954034
State: [625x1 uint32]
>> rand(1,4)          %New random matrix produced with new seed
ans =

0.5706    0.9136    0.4043    0.8423```

As demonstrated in the above code, the settings of the random number generator rng can be saved and restored. The type, seed, and state of rng can always be accessed as well.

## Alternatives to the Matlab rand function

The current version of Matlab uses the Mersenne Twister (MT) algorithm to generate pseudorandom numbers by default. Other algorithms can be selected with the rng function. The MT algorithm is used in many programming languages, including C++, Python, R, and Ruby, and is sufficiently robust for most engineering applications, such as Monte Carlo simulations. The rng function can generate pseudorandom numbers with other algorithms, including a combined multiple recursion number generator and a multiplicative lagged Fibonacci number generator.

To obtain numbers that are more random and independent, functions provided by various operating systems can be used. CryptGenRandom is the default Windows function used to provide random numbers that are meant to be sufficiently robust for cryptography. Despite being secret, the algorithm from Windows 2000 was found to be vulnerable to various attacks in 2007. In mid-2008, Microsoft released a patch to fix this vulnerability. Unix-like operating systems use /dev/random to generate random or pseudorandom numbers by harvesting entropy from the environment. This approach is also meant to be sufficiently robust for cryptography but has also been found to have flaws.

There are other pseudorandom and random number generators that are not linked to an operating system. The Fortuna algorithm can be used for cryptographic applications, but its use for engineering applications is unjustified because of its slow speed. The website Random.org claims to provide “truly” random numbers based on atmospheric noise. There is a Matlab script that can access random numbers from this site, which can be found on Matlab Central. If you use this script, remember that there is a quota of up to 1 million random bits for free usage.

In addition, there are other pseudorandom and random number generators that can be used for various numerical computing applications, which are beyond the scope of this blog post. In the next post, I will explain how to create more distributions by using the basic rand and randn functions provided by Matlab.

## 2 thoughts on “Random Numbers in Matlab – Part II”

1. A to była dopiero kropla w morzu potrzeb.