Skip to content

thinknathan/defold-prng

Repository files navigation

Def-Mulberry32

Mulberry32 PRNG for Defold

Build with bob GitHub License

This Defold native extension implements a seeded psuedo-random number generator using the Mulberry32 method.

Usage

-- Set the seed. Must be an integer.
prng.set_seed(12345)

-- Generate a random float between 0 and 1.
local randomFloat = prng.rand_float()

-- Generate a random integer between 0 and the maximum you provide.
local randomInt = prng.rand_int(5)

-- Flips a coin. Returns 0 or 1.
local coinFlip = prng.coin()

-- Get a card suit. Returns 0, 1, 2, or 3.
local randomCardSuit = prng.suit()

-- Roll one or more dice.
local dice = prng.dice(1, 8)

-- Rolls twice and takes the higher result. More likely to return 1 than 0.
local success = prng.advantage()

-- Rolls twice and takes the lower result. More likely to return 0 than 1.
local success = prng.disadvantage()

Installation

  1. Edit game.project
  2. Add dependency https://github.com/thinknathan/defold-prng/archive/main.zip for the current version

TypeScript Definitions

This extension includes types for use with TypeScript + Defold.

  1. Install these types
yarn add git+https://git@github.com/thinknathan/defold-prng.git#^1.0.0 -D
# or
npm install git+https://git@github.com/thinknathan/defold-prng.git#^1.0.0 --save-dev
  1. Add defold-prng to types in tsconfig.json
{
	"compilerOptions": {
		"types": [
+			"defold-prng",
		],
	}
}
  1. Add node_modules/@types to typeRoots in tsconfig.json if it's not already there
{
	"compilerOptions": {
		"typeRoots": [
+			"node_modules/@types",
		],
	}
}

Alternatives

See SplitMix64, PCG Random and rand16. Compare PRNG implementations.