rng.lua

From PGE Wiki
Jump to: navigation, search


Attention: This page contains dead links!

Download Latest Version

Lua's built in random number generator math.random doesn't always generate nice random numbers. This library implements its own random number generator, which is much more consistent.

Installation

Place the file rng.lua into the LuaScriptsLib folder.

How to use

To enable the RNG library for a specific level, add this line to lunadll.lua:

local rng = API.load("rng");

This will load the RNG API.

Example

local rng = API.load("rng");
    
function onLoad()
    local x = rng.random();
    --Generates a random decimal number between 0 and 1.
end

Documentation

Constants

These are static values that should be treated as read-only

MAX number The maximum representable number. This can vary depending on the machine running the code.

Values

These are variables you can access

seed number The seed for the random number generator. Initially, this is nil. If you try to generate a random number while this value is nil, it will be automatically generated. Setting this value allows you to ensure a specific random sequence is generated.

External Use Functions

These are functions you will need to use the library

random
Generates a random decimal number between 0 and 1, inclusive. number
random limit
Generates a random decimal number between 0 and the specified limit, inclusive. number number

The maximum value that can be generated.

random lower limit upper limit
Generates a random decimal number between the specified lower limit and upper limit, inclusive. number number

The minimum value that can be generated.

number

The maximum value that can be generated.

randomInt
Generates a random integer between 0 and 1,000,000, inclusive. number
randomInt limit
Generates a random integer between 0 and the specified limit, inclusive. number number

The maximum value that can be generated.

randomInt lower limit upper limit
Generates a random integer between the specified lower limit and upper limit, inclusive. number number

The minimum value that can be generated.

number

The maximum value that can be generated.

randomEntry table
Picks a random element in the given table. object table

The table to pick an element from.

irandomEntry table
Picks a random element in the given table, assuming the table is an array with no named fields and no gaps. object table

The table to pick an element from.

randomChar
Generates a random letter, A-Z, either upper case or lower case. string
randomChar uppercase
Generates a random letter, A-Z. If uppercase is true, then the letter will be upper case, otherwise it will be lower case. string boolean

Should the generated letter be upper case or lower case?

randomChar limit
Generates a random letter between "A" and the specified letter (e.g. randomChar("Z")). Note that these ranges are based on ASCII codes. string string

The last letter that can be generated

randomChar lower limit upper limit
Generates a random letter between the lower and upper limits (e.g. randomChar("A","Z")). Note that these ranges are based on ASCII codes. string string

The first letter that can be generated

string

The last letter that can be generated