SplitMix64 PRNG

logo

Hi everyone!

I’m happy to announce the final release of the SplitMix64 extension for Defold - fast, but non-cryptographic PRNG. While this extension has been around for over 5 years and personally we have been using the extension in all our games, it previously offered only a minimal API. This update turns it into a more flexible and robust tool!

What’s new:

  • State Management: You can now get and set the PRNG state. This is essential for save games, allowing you to restore the exact sequence of “random” numbers.
  • Multiple Instances: You are no longer limited to a single global state. You can now create multiple independent instances of the generator to use in different modules of your game without them interfering with each other.
  • Additional Helper Functions!

GitHub: GitHub - indiesoftby/defold-splitmix64: SplitMix64 PRNG for Defold: get the same random numbers from the same seeds on all platforms supported by Defold. · GitHub

Example:

-- Create two independent generators
local rng_enemies = splitmix64.new_instance(42)
local rng_loot = splitmix64.new_instance(999)

-- Each has its own state — they don't affect each other or the global module
print(rng_enemies.random(1, 100))
print(rng_loot.random(1, 100))

-- The global state is unchanged
print(splitmix64.random())

-- Save/restore instance state
local saved = rng_enemies.state()
rng_enemies.randomseed(saved)

Happy Defolding! :comet:

23 Likes

Another useful thing I wanted to mention - the implementations/** directory in the project contains reference implementations of the SplitMix64 algorithm for other languages:

  • C99
  • Python 3
  • TypeScript

These implementations produce identical random number sequences for the same seeds, which makes them suitable for networked multiplayer games that rely on deterministic PRNG state. By seeding all clients with the same value, you can guarantee consistent simulation results across platforms and languages!

5 Likes

Seems very useful! I wasn’t aware that on different platforms you get different results from the same seed. Thank you!

2 Likes