MIND BLOG

[UEFN/Verse Language] Seeded Pseudo-Random Number Generator

Person who needs help
Person who needs help

I would like to implement a seeded pseudo-random number generator in Verse language!

We can help you with your concerns.

Reliability of This Article
by Our Founder/CEO&CTO Hiroyuki Chishiro

  • He has been involved in 12 years of research on real-time systems.
  • He taught OS (Linux kernel) in English at the University of Tokyo.
  • From September 2012 to August 2013, he was a visiting researcher at the Department of Computer Science, the University of North Carolina at Chapel Hill (UNC), Chapel Hill, North Carolina, United States. He has been involved in research and development of real-time Linux in C language.
  • He has more than 15 years of programming experience in C/C++, Python, Solidity/Vyper, Java, Ruby, Go, Rust, D, HTML/CSS/JS/PHP, MATLAB, Verse (UEFN), Assembler (x64, ARM).
  • While a faculty member at the University of Tokyo, he developed the "Extension of LLVM Compiler" in C++ language and his own real-time OS "Mcube Kernel" in C language, which he published as open source on GitHub.
  • In January 2020-Present, he is CTO of Guarantee Happiness LLC, Chapel Hill, North Carolina, United States, in charge of e-commerce site development and web/social network marketing. In June 2022-Present, he is CEO&CTO of Japanese Tar Heel, Inc. in Chapel Hill, North Carolina, United States.
  • We have been engaged in disseminating useful information on AI and Crypto (Web3), and working on game development with Unreal Editor for Fortnite (UEFN).
    • We have written more than 20 articles on AI including AI chatbots such as ChatGPT, Auto-GPT, Gemini (formerly Bard). He has experience in contract work as a prompt engineer, manager, and quality assurance (QA) for training ChatGPT/Gemini in several companies in San Francisco, United States (Silicon Valley in the broadest sense of the word).
    • We have written more than 40 articles on cryptocurrency (including smart contract programming). He has experience as an outsourced translator of English articles on cryptocurrency into Japanese for a company in London, England.
    • We have developed more than 10 games on UEFN and published on Fortnite (Fortnite, Fortnite.GG).

You can learn from us.

This article explains how to implement a seeded pseudo-random number generator in UEFN/Verse.

When creating random dungeons and similar systems in UEFN, you may want to reproduce the same generated result during debugging.

In this article, we implement a pseudo-random number generator named my_seeded_random_generator that allows you to specify a seed in Verse.

For production gameplay, we use the standard Verse Random module.

This implementation is mainly intended for debugging and reproducibility testing.

What Is a Seeded Pseudo-Random Number Generator?

A seeded pseudo-random number generator generates pseudo-random numbers from an initial value called a seed.

The seed is used to initialize the internal state of the pseudo-random number generator. The internal state is the data retained by the generator to calculate the next pseudo-random number.

If you set the same seed and generate random numbers in the same order, you can reproduce the same results.

For example, suppose a certain seed generates the following values:

If you set the same seed again, the same pseudo-random number sequence is generated.

The Verse Random module provides the following functions:

GetRandomInt() and GetRandomFloat() generate random numbers, while Shuffle() randomly rearranges the elements of an array.

However, the Random module does not provide a public function for setting an arbitrary seed.

Therefore, if you want to reproduce the same generated results, you need to implement a separate seeded pseudo-random number generator.

Use Cases for a Seeded Pseudo-Random Number Generator

The main use case for a seeded pseudo-random number generator is debugging.

For example, suppose a random dungeon becomes impossible to complete only with a particular seed. By fixing the seed, you can generate the same dungeon repeatedly.

It can also be useful for:

  • Reproducing random dungeons
  • Reproducing enemy and trap placement
  • Testing item rolls
  • Reproducing bugs
  • Regression testing
  • Fixed maps such as a Daily Seed

However, when reproducibility is not required in production gameplay, use the standard Verse Random module.

Why the Mersenne Twister Is Difficult to Implement in Verse

The Mersenne Twister is a high-quality pseudo-random number generator, but it makes extensive use of bitwise operations.

For more information about the algorithm, see Mersenne Twister on Wikipedia.

It uses operations such as:

In Verse, integer bitwise operations cannot be used as directly as they can in C or C++.

Therefore, these operations need to be reproduced manually.

Reproducing bitwise operations with per-bit loops makes the implementation more complicated and increases the amount of processing required.

Why We Chose MRG32k3a for a Seeded Pseudo-Random Number Generator

MRG32k3a is a method proposed by Pierre L'Ecuyer in 1999 that combines two multiple recursive pseudo-random number generators.

Its main characteristics are:

  • No bitwise operations are required
  • It can be implemented using integer multiplication, subtraction, and modulo operations
  • The internal state consists of six integers
  • Its period is approximately \(2^{191}\)
  • It can be implemented relatively efficiently in Verse

For debugging random dungeons, it provides a sufficiently long period and sufficient random-number quality.

Compared with the Mersenne Twister:

ItemMRG32k3aMersenne Twister
Main operationsMultiplication, subtraction, moduloBitwise operations
Internal state6 values624 values
Implementation in VerseRelatively simpleComplex
PeriodSufficiently longExtremely long

For use in Verse, we decided that MRG32k3a would be easier to implement.

Implementing my_seeded_random_generator in Verse

The class and functions implemented in this article are:

We prefix the function names with My to distinguish them from the functions provided by the standard Verse Random module.

Our implementation is shown below.

MyGetRandomInt() assumes that the requested integer range does not exceed the output range of one MRG32k3a value.

How to Use my_seeded_random_generator

The following example demonstrates how to use my_seeded_random_generator.

It first verifies that the same seed produces the same value, and then uses MyGetRandomInt(), MyGetRandomFloat(), and MyShuffle().

FirstValue and SecondValue have the same value.

After that, an integer, a floating-point value, and a shuffled array are output.

With this implementation, if you use the same seed and the same function call order, the output is always as follows:

The output from the Print function can be viewed under Verse Diagnostics in the Message Log.

The file path, line number, timestamp, and other log information have been omitted.

Important Notes When Using my_seeded_random_generator

When using my_seeded_random_generator, pay attention to the order in which pseudo-random numbers are generated.

Even if you use the same seed, changing the order or number of calls to the random-number functions changes the pseudo-random numbers generated afterward.

For example, suppose pseudo-random numbers are used in this order:

  1. Room
  2. Enemy
  3. Trap

If a random-number operation for decorations is inserted:

  1. Room
  2. Decoration
  3. Enemy
  4. Trap

The number of calls to the random-number functions has increased by one, so the pseudo-random numbers used for enemies and traps also change.

For this reason, in a large random dungeon, we recommend using separate pseudo-random number generators for different purposes.

Also, random-number generation on the same my_seeded_random_generator instance is not reentrant.

Reentrant means that a procedure can be invoked again before a previous invocation has completed without the invocations interfering with each other.

Because the generator modifies its internal state every time it generates a pseudo-random number, using the same instance from multiple concurrent operations may cause the result to depend on the order in which random numbers are generated.

When using the generator in concurrent operations, use a different my_seeded_random_generator instance for each operation.

Reference: Comparison with the Epic Developer Community Pseudo-Random Number Generator

The Epic Developer Community has another seeded pseudo-random number generator implemented by TyrantKingBen.

That implementation uses a string seed and SFC32.

SFC32 itself is a lightweight pseudo-random number generator, but it uses bitwise operations.

Therefore, its Verse implementation reproduces operations such as:

Because the bitwise operations are reproduced using loops, the Verse implementation requires more processing.

The implementations can be compared as follows:

ItemMRG32k3a Implementation in This ArticleEpic Developer Community SFC32 Implementation
Seedintstring
Main operationsMultiplication, subtraction, moduloXOR, shifts, OR
Bitwise-operation emulationNot requiredRequired
Processing in VerseRelatively lowRelatively high
String seedNot supportedSupported
DebuggingSuitableSuitable

If you want to use a string directly as a seed, the SFC32 implementation is convenient.

On the other hand, if an integer seed is sufficient and you want to avoid emulating bitwise operations in Verse, MRG32k3a is a suitable choice.

Summary

This article explained how to implement a seeded pseudo-random number generator in UEFN/Verse.

For debugging random dungeons and similar systems, use my_seeded_random_generator.

For production gameplay, use the standard Verse Random module.

If you'd like to play the game we developed using the Unreal Editor for Fortnite (UEFN) in Fortnite, click the following.

UEFN Fortnite Game
Our Game in Unreal Editor for Fortnite (UEFN) for Fortnite

You can learn from us. We introduce our game in Unreal Editor for Fortnite (UEFN) for Fortnite. Let's come and play our games! If you want to learn UEFN first, please click the following. March 20, 2024: 🌸🥚Spring Easy Deathrun 250+🌸🥚 [Island Code: 7022-3666-7030] 🌸🥚Spring Easy Deathrun 250+🌸🥚 is a spring-themed deathrun (parkour) game. Deathrun is an action game like a 3D version of Super Mario. You can enjoy cherry blossoms and Easter eggs! https://www.youtube.com/watch?v=zPpiu_hDqTo If you want to play this game, copy the following Island Code "7022-3666-7030" and search for it in Fortnite! April 3, 2024: ✒Math Times Table ...

-MIND, BLOG
-, , , , ,