Lua-Rng

TEMPLATE NAME: Lua-Rng
CORE VERSION: 1.0.285
TEMPLATE VERSION: 1.0.0

TEMPLATE DESCRIPTION:

Lua 5.3 implementation of Philox counter-based random number generator.

John K. Salmon, Mark A. Moraes, Ron O. Dror, and David E. Shaw, "Parallel Random Numbers: As Easy as 1, 2, 3," Proceedings of the International Conference for High Performance Computing, Networking, Storage and Analysis (SC11), New York, NY: ACM, 2011. https://www.thesalmons.org/john/random123/

TEMPLATE ROADMAP:

  • (no current plans)

TEMPLATE VERSION NOTES:

Version 1.0.0

  • First stable release.

ADDITIONAL INFO:


Getting Started

Import

  1. In Community Content, search for "descendent".
  2. Browse to the listing for "Lua-Rng", and click "Import"; then click "Yes".
  3. In Core Content, under the "Community Content" category, select the "Imported Content > Lua-Rng" group.
  4. Add the "Lua-Rng" template to the Hierarchy (in Default Context); then delete the new template instance from the Hierarchy.
    • This step is necessary to import this package's assets into Project Content.

How-To Guides

Use the "Philox" Module in a Script

  1. In Project Content, browse to the script that will be using the "Philox" module, and select it.
  2. In the Properties panel, add a custom property of type "Asset Reference", and name it "Philox"; then set its default value to reference the "Philox" script.
  3. Edit the script that will be using the "Philox" module; at or near the top of the code, add…
    local Philox = require(script:GetCustomProperty("Philox"))
    
  4. Create and use Philox instances as-needed. (See Philox API reference.)

Update

  1. In Project Content, right-click the "Imported Content > Lua-Rng" group, and select "Download Latest"; then click "Yes"; then click "Continue with save: I accept the risk".

Delete

  1. In Project Content, right-click the "Imported Content > Lua-Rng" group, and select "Delete Assets"; then, if asked, click "Delete All and Save" each time; then click "Delete All" each time.
  2. In Core Content, under the "Community Content" category, right-click the "Imported Content > Lua-Rng" group, and select "Delete Assets"; then click "Delete All" each time.

API Reference

Philox

Constructors

Philox New(integer key)
Philox New(table key)
Philox New(integer key, table counter)
Philox New(table key, table counter)

Creates and returns a new Philox instance, with seed key, and state counter. If counter isn't given, it will be {0, 0, 0, 0}. If key isn't an integer, it must be an array-like table of 2 unsigned integers less than 232 (representing a single 64-bit unsigned integer). counter must be an array-like table of 4 unsigned integers less than 232 (representing a single 128-bit unsigned integer).

Static Methods

nil ConfigureDebug(boolean value)

If value is false, most assertions in methods will be skipped; otherwise, all assertions will be checked. By default, all assertions will be checked. Skipping assertions will slightly increase performance, but will also make the cause of errors more difficult to pinpoint.

table Philox_4x32_10(table counter, table key)

Returns an array-like table of 4 random, unsigned integers less than 232, generated using the Philox counter-based random number generator algorithm, with key key, counter counter, and 10 rounds. key must be an array-like table of 2 unsigned integers less than 232 (representing a single 64-bit unsigned integer). counter must be an array-like table of 4 unsigned integers less than 232 (representing a single 128-bit unsigned integer).

Methods (Accessors)

table GetKey()

Returns this Philox instance's seed as an array-like table of 2 unsigned integers less than 232 (representing a single 64-bit unsigned integer).

table GetCounter()

Returns this Philox instance's state as an array-like table of 4 unsigned integers less than 232 (representing a single 128-bit unsigned integer).

Methods

nil Step()
nil Step(integer count)

Increments this Philox instance's state by count. If count isn't given, it will be 1. count must be an unsigned integer greater than 0, and less than 232.

nil Jump()

Increments this Philox instance's state by 264.

float Next()

Returns a random float greater than or equal to 0.0, and less than 1.0, following a uniform distribution. Increments this Philox instance's state at the end of the second call after each time this Philox instance's state is initialized or changed.

Examples

Usage

ExampleClient (Client Context)

Custom Properties

AssetReference Philox

Reference to "Philox" script.

Code

local Philox = require(script:GetCustomProperty("Philox"))

--local _rng = Philox.New(DateTime.CurrentTime().millisecondsSinceEpoch)
local _rng = Philox.New(1672549200000)

print(_rng:Next())
print(_rng:Next())
print(_rng:Next())

-- Random angle (in degrees)
print(_rng:Next() * 360.0)
print(_rng:Next() * 360.0)
print(_rng:Next() * 360.0)

-- Random angle (in degrees) greater than or equal to 45.0, and less than 135.0
print(45.0 + (_rng:Next() * (135.0 - 45.0)))
print(45.0 + (_rng:Next() * (135.0 - 45.0)))
print(45.0 + (_rng:Next() * (135.0 - 45.0)))

-- Random integer 1–100
print(math.floor(1 + (_rng:Next() * 100)))
print(math.floor(1 + (_rng:Next() * 100)))
print(math.floor(1 + (_rng:Next() * 100)))

:receipt: (Event Log)

0.70412068382327
0.035012175855091
0.49512467875001
190.21300870166
175.84232993457
105.67664541339
53.2981959591
103.89078449904
65.377112548898
56
22
33