Skip to Content
defold-typescript libraries
indiesoftby

indiesoftby/defold-splitmix64/splitmix64

Functions for interacting with the SplitMix64 native extension

  • GitHub: indiesoftby/defold-splitmix64 — pinned to 2.2.0 (released while Defold 1.12.2 was current)
    1. Pick a release from indiesoftby/defold-splitmix64 releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as splitmix64 from "splitmix64"
  • Asset Portal: defold-splitmix64

Functions

splitmix64.random(m?: number, n?: number): number

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

splitmix64.randomseed(x: number | string)

Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers. Accepts a number or a string (decimal representation of uint64) for full 64-bit precision.

Parameters

  • x: number | string — Seed value as number or string (e.g. "18446744073709551615" for full uint64 range).

splitmix64.state(): string

Returns the current internal state x as a string. Use this to save/restore state or pass to randomseed for full 64-bit precision (Lua double cannot represent all uint64 values).

Returns

  • state: string — Current state as decimal string.

splitmix64.randomchoice(t: table): any

Returns a random value from array t. If the array is empty an error is raised.

Parameters

  • t: table — Array of values to choose from.

Returns

  • value: any — Random element from the array.

splitmix64.weightedchoice(t: table): any

Takes table t where keys are choices and values are weights. Returns a random key. Keys are sorted deterministically (numbers first, then strings lexicographically) before selection, so the result is reproducible for the same seed. Weights must be >= 0. If all weights are 0 or a weight is negative, an error is raised.

Parameters

  • t: table — Table of key-weight pairs.

Returns

  • key: any — Random key chosen by weight.

splitmix64.toss(): number

Toss a coin. Returns 0 or 1.

Returns

  • value: number — Returns 0 or 1.

splitmix64.new_instance(seed?: number | string): userdata

Creates a new RNG instance with independent state. Optional seed (number or string) for reproducibility.

Parameters

  • seed?: number | string — Optional seed for the new instance.

Returns

  • rng: userdata — RNG instance with methods random, randomseed, state, randomchoice, weightedchoice, toss, dice, shuffle. Same API as the module but with isolated state.

splitmix64.dice(roll: number, type: number): LuaMultiReturn<[table, number]>

Roll one or more dice of the given type. Returns a table of individual rolls and the total sum.

Parameters

Returns

  • rolls: table — Table of individual roll results.
  • total: number — Sum of all rolls.

splitmix64.shuffle(t: table, inplace?: boolean): table

Shuffles an array using the Fisher-Yates algorithm. Returns a shuffled copy of the array, or if inplace is true, shuffles the original array in-place and returns it.

Parameters

  • t: table — Array to shuffle.
  • inplace?: boolean — If true, shuffle the array in-place instead of creating a copy. Defaults to false.

Returns

  • result: table — The shuffled array (a new table, or the same table if inplace is true).

Constants

splitmix64.D4

Dice type constant for 4-sided die.

splitmix64.D6

Dice type constant for 6-sided die.

splitmix64.D8

Dice type constant for 8-sided die.

splitmix64.D10

Dice type constant for 10-sided die (0-9).

splitmix64.D12

Dice type constant for 12-sided die.

splitmix64.D20

Dice type constant for 20-sided die.

splitmix64.D100

Dice type constant for percentile die (0, 10, 20, ..., 90).

On this page