TIS-100 Hacker's Guide

math.random() and math.randomseed()

In Puzzle Lua math.random(min,max) and math.randomseed(seed) are not from the main Lua distribution, which is based on the C library's rand() and srand(int).

Instead they come from MoonSharp, a Lua interpreter written in C#. That is calling into Unity's System.Random in Random.cs. That, and thus TIS-100, is based on ran3() from Numerical Recipes in C, Second Edition. That is based on a random number generator by Donald Knuth in Seminumerical Algorithms, second edition, volume 2 of The Art of Computer Programming, sections 3.2-3.3.

Differences from the Numerical Recipes version (besides the interface): 1. MBIG has been replaced with 2147483647 , which is 231-1, or the largest 32-bit signed integer using two's complement. 2. The outer labs() call in the initial calculation of mj was removed.

Like math.random, the order of the arguments doesn't matter. Similarly to Lua, if a single argument is given, it behaves as if 1 was given as the second argument. Unlike Lua, a single negative argument is valid, so math.random(-2) is equivalent to math.random(-2,1), while official Lua would fail with "bad argument #1 to 'random' (interval is empty)". Given no arguments, returns 0. The reference C# linking Random.cs to Lua is in MathModule.cs.

Implementations:

A small test suit for a C or Lua implementation of the random functions is available in the "random" subdirectory in this project at GitHub.

Many thanks to Rohansi on Reddit for discovering the MoonSharp to Unity to C# link and pointing me at the C# implementation. Many, many thanks to AapOpSokken on Reddit for extensive additions and catching of details.