Resource icon

[Open Assets] Lightweight high-quality RNG (LRNG) 1.1

What permissions do you give others to modify and/or maintain your submission?
Modify: YES - Maintain: YES - I give permission for my entire submission to be modified by others or used in their own work. I give permission for my entire submission to be maintained by others as well.
This library (LRNG for short) is a minimalistic, high-quality RNG library that you can incorporate into your own code.
It is based on the SFC32 PRNG algorithm, which passes rigorous statistical tests while still being very fast. It is designed to be simple above all else: there are no protections, and states are simply Lua tables containing 4 numbers.

List of functions:
  • LRNG.NextInt(state): Returns the next INT32 produced from the state. It can be negative.
  • LRNG.Seed(state, ...): Takes up to 3 numbers to use as seeds and seeds the state provided in place; that is, this function does not return anything. Seed values past the third one are ignored.
  • LRNG.New(...): Helper function that creates a new state with the given seeds.
  • LRNG.NextPositive(state): Returns a non-negative random number.
  • LRNG.NextFixed(state): Returns a number between 0 and FRACUNIT -1.
  • LRNG.NextKey(state, limit): Returns a random number on the interval [0, limit). limit must be positive but can be up to INT32_MAX.
  • LRNG.NextRange(state, min, max): Returns a random number on the interval [min, max]. min must be less than max and can otherwise have any value.
  • LRNG.NextBits(state, bits): Returns an unsigned integer of 1-31 bits or a signed integer of 32 bits based on the bits value. (new in 1.1)
  • LRNG.Shuffle(state, table): Shuffles the integer part of a table. Modifies the table in place and so returns nothing. (new in 1.1)

As a bonus, here's a minimal SFC32 implementation. This isn't compatible with the full library but if you want a good RNG that's easy to drop into another script, it might be handy.
Minimal SFC32 implementation:
--Same as calling RN_Seed(1, 0, 0)
local x, y, z, ctr = 1966444699, -108448308, -1945534430, 17

--Returns 31 bits of random data as a positive integer.
local function RN_NextInt()
    local b, c, counter = y, z, ctr
    local output = x + b + counter
    ctr = counter + 1
    x = b ^^ (b >> 9)
    y = c * 9
    z = output + ((c << 21) | (c >> 11))
    return output >> 1
end

--Reseeds the random number generator with up to 3 numbers.
local function RN_Seed(...)
    local fallback_seeds = {1, 69105, 4142001}
    local seeds = {...}
    for i=1, 3 do
        seeds[i] = seeds[i] or fallback_seeds[i]
    end
    x, y, z, ctr = seeds[3], seeds[2], seeds[1], 1
    for i=1,16 do
        RN_NextInt()
    end
end

rawset(_G,"RN_NextInt",RN_NextInt)
rawset(_G,"RN_Seed",RN_Seed)
Author
tertu
Downloads
247
Views
1,206
Extension type
lua
File size
2.3 KB
MD5 Hash
f166aae68d85d4ebffb431240de3b8a6
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from tertu

Share this resource

Latest updates

  1. Version 1.1

    Name change, also new functions: LRNG.Shuffle and LRNG.GetBits.
Back
Top