The Mersenne Twister: A Reliable PRNG


What does it mean to be random? And how do you achieve randomness in a computer? Randomness is a peculiar concept defined more by what it isn’t than what it is. If you can spot a pattern or predict the next outcome, you’d typically say, “Well, that’s not random.” In essence, true randomness means no discernible pattern, and it should be unpredictable. This poses a challenge for computers since their fundamental design is rooted in determinism. Computers execute instructions sequentially, step by step, ensuring the same output for the same input.

Still, computers often need to use random numbers for games, keeping data safe, and other tasks. To do this, they use something called a Random Number Generator (RNG). There are different types of RNGs. Some use math to create numbers that seem random but might repeat after some time. One famous RNG is the Mersenne Twister.

In video games, RNGs can make different things happen each time you play. But, sometimes players can see patterns, like in the game “No Man’s Sky.” To make sure RNGs work well, we can use tests called Monte Carlo simulations.

Computers can’t make things truly random, but they try their best because it’s important for keeping information private.

Computers also keep our data safe with something called encryption. It’s like turning data into a secret code, which can only be understood if you have a special key. The safety of this secret code is very important. We trust this code to protect our personal and money information on the internet.

Today, there’s a strong encryption method called SSL/TLS that keeps our data safe from thieves. SSL/TLS encryption plays a vital role in ensuring the privacy and integrity of online communications, allowing us to share personal and financial information securely. As computer performance increases, ongoing research and development in the field of cryptography remain essential to stay ahead of potential threats and maintain sensitive data integrity. Just as we trust the security of our encrypted communications, we also rely on the unpredictability of certain digital processes, such as random number generation, to protect us. In the future, very powerful computers might be able to break this code, but for now, it’s our best guard.

In short, encryption is about making data secret, while randomness makes data unpredictable. Both are important for computers to keep things safe. 

Going back to the topic of how computers can generate random numbers, what if we actually wanted a computer to generate truly random numbers? The answer lies in pseudorandomness.

Pseudorandomness: The Illusion of Randomness

In the world of computing, we often use what’s known as pseudorandomness. It’s not truly random since, given the same inputs and program, you can predict the output. However, pseudorandom number generators aim to eliminate any discernible patterns, achieving the illusion of true randomness.

Let’s take a closer look at this concept. Imagine a magical machine that claims to generate random numbers. We press a button, and it produces a number, say 90. Is 90 truly random? Well, it depends on what you define as a possible event. Randomness needs a predefined set of potential outcomes. For instance, in this case, it might be two-digit decimal numbers.

But what if we want a coin flip instead? We could decide that even numbers represent “heads,” and odd numbers represent “tails.” The crucial point here is that we can’t discuss randomness without first defining a set of possible outcomes.

So, how do computers achieve pseudorandomness? They use algorithms known as pseudorandom number generators (PRNGs). Let’s explore one of the most famous PRNGs: the Mersenne Twister.

The Mersenne Twister: A Reliable PRNG

The Mersenne Twister is a well-established pseudorandom number generator that has been used for various applications. Unlike some primitive generators, the Mersenne Twister provides a more reliable and less predictable source of pseudorandom numbers.

How it Works

The Mersenne Twister is built around a significant amount of internal state. This state consists of a large array of bits that the algorithm continually manipulates. When you request a random number, it draws from this internal state, mixing and modifying the bits to produce the desired output.

This extensive internal state is one of the reasons the Mersenne Twister is considered a better choice for pseudorandom number generation. With a larger state space, it takes much longer for the generator to repeat itself and show patterns.

Reseeding

One essential aspect of using PRNGs like the Mersenne Twister is seeding. Seeding sets the initial state of the generator, and it’s crucial for producing different sequences of numbers. However, there’s a caveat: if you reseed the generator repeatedly with the same value, you’ll get the same sequence of “random” numbers. In essence, the sequence becomes predictable.

RNG with Mersenne Twister in Java

We’ve explored the concept of pseudorandomness and the Mersenne Twister algorithm. Now, let’s dive into its Java 11 implementation.

Let’s think of our project as if we are going to operate one of the symbol reels we have for the slot game. In slot games, each symbol reel represents a slot wheel.

Let’s take a symbol reel of a slot game as an example, a large array of symbols.

We use the random number to select a random index from this symbol reel array and arrange it.

For example, when the RNG value is 8, one wheel will have the following symbols, considering that the slot game has 5 visible symbols:

This is an array and the index starts from 0, and index 8 starts from symbol 3. Now we know where and how to use RNG in the slot game.


In many applications, like games and random number generators, we need a starting value or “seed”. While it’s tempting to get these seeds from an external server, it’s crucial to ensure the source is trustworthy. This is especially true when dealing with cryptocurrencies or security-sensitive tasks. In gaming, however, seeds are commonly sourced from reliable remote servers.

Now, let’s create a Python-based web server to generate seed, producing numbers between 0 and 99999999.

Use the following terminal commands to run this server from which we will get seeds.

You can use a terminal, postman, or browser to test. You will see that the server returns the seed as follows.

Now we can move on to the Java part. First, we create the Project with Maven.

We create the SeedRequest class and then, add the request code below to get the seed from the remote server.

Please add the following dependencies to your pom.xml file.

In order to use the MersenneTwister library, we first need to import it.

At this stage, we create the RandomNumberGenerate class. Please remember the logic we explained in the first place for the slot game to work here. We have a symbol reel and we need an RNG for it.

We add the getSeed method to get seeds from the remote server, here we create an instance of the SeedRequest class and process the error just in case.

Our second method, randomNumberGenerate, takes a parameter called realLine of type int. Under normal circumstances, we expect this to be an array of type int (because we will always have a symbol reel: [1,5,7,3,2….] But here we focus on RNG. We will give an int number and see what happens.

To run the project, we create an instant from the RandomNumberGenerate class in the Main class. We assume that we have a reel consisting of 125 symbols and we give 125 as a parameter to the randomNumberGenerate method.

The output will be as follows:

Yes, after a long journey, we now have our random number. When we have a reel consisting of symbols in a slot game, we take this random number as an index and arrange it according to the number of symbols that appear. This is exactly the random number that ensures that the game is unbiased and fair.

Advantages of the Mersenne Twister:

  • Long Periodicity: The Mersenne Twister can produce an extensive sequence of unique pseudorandom numbers before it repeats. This makes it great for simulations, statistics, and cryptography.

  • Good Statistical Properties: It demonstrates strong statistical qualities, ensuring uniformity and independence between numbers. This versatility suits various statistical and modeling tasks.

  • Reproducibility: When initialized with the same seed, the Mersenne Twister reliably generates the same pseudorandom sequence. This feature is valuable for debugging and testing.

  • Easy Initialization: Initializing the Mersenne Twister with a seed is a straightforward process, making it user-friendly and adaptable.


Conclusion

While computers can’t achieve true randomness, pseudorandom number generators like the Mersenne Twister provide a reliable illusion of it. With proper seeding and understanding, developers can harness this power for various applications, from games to cryptographic solutions.

Resources

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics