Skip to content

SplitMix64 PRNG for Defold: get the same random numbers from the same seeds on all platforms supported by Defold.

License

Notifications You must be signed in to change notification settings

indiesoftby/defold-splitmix64

Repository files navigation

SplixMix64 Logo

SplitMix64 PRNG for Defold

This extension wraps SplitMix64 in a simple Lua API for the Defold game engine. The SplitMix64 algo is from http://prng.di.unimi.it/splitmix64.c, and it's a very fast generator passing BigCrush.

The main idea of this extension is to get the same random numbers from the same seeds on ALL platforms supported by Defold.

Take a look at the web build 🐲 of the included example, which tests random values from the same seeds.

Splitmix64 is the default pseudo-random number generator algorithm in Java and is included / available in many other languages. It uses a fairly simple algorithm that, though it is considered to be poor for cryptographic purposes, is very fast to calculate, and is "good enough" for many random number needs. It passes several fairly rigorous PRNG "fitness" tests that some more complex algorithms fail.

Installation

You can use it in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

https://github.com/indiesoftby/defold-splitmix64/archive/main.zip

Or point to the ZIP file of a specific release.

Usage

splitmix64.randomseed(x)

Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers.

PARAMETERS

  • x number.

splitmix64.random([m],[n])

When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, splitmix64.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, splitmix64.random returns a uniform pseudo-random integer in the range [m, n].

PARAMETERS

  • m number.
  • n number.

RETURNS

  • value number - A pseudo-random number.

Example

splitmix64.randomseed(1)

print(splitmix64.random())
print(splitmix64.random(1, 10000))

Advanced Usage

You can also globally substitute the built-in math.random with splitmix64:

math.randomseed = splitmix64.randomseed
math.random = splitmix64.random