This library implements the complementary multiply-with-carry algorithm created by George Marsaglia as a LuaRock.
It is designed for use in games. It is meant to be deterministic: given a certain intial seed, the sequence of results provided by the library should be the same.
Install using LuaRocks:
luarocks install mwcrng
That allows you to access this library in your Lua files/interpreter:
mwcrng = require("mwcrng")
Alternatively, if running Lua as a C library, you could run this function on your Lua State:
luaopen_mwcrng(L);
Initializes the library, and must be called before any other function. The argument must be an integer. The result of os.time()
is a good choice of seed if you don't have a particular one you wish to use.
Simulates a coin toss. Has a 50% chance of returning true, and a 50% chance of returning false.
Simulates throwing throws
number of dice, each with an equal chance of providing any result from 1 to sides
inclusive. The result is the sum of all the values of all the simulated dice.
For example, dice(1,6)
would return any value from 1 to 6 inclusive with equal probability.
dice(2,4)
would return any value from 2 to 8 inclusive, but not with equal probability: values of exactly 2 and 8 would be relatively unlikely.