math


Functions

imod(index : integer, length : integer)

->integer

Wrap a lua 1 based integer index into the given array/table length.

-> (index - 1) % length + 1

random(m : integer, n : integer)

->integer

  • math.random(): Returns a float in the range [0,1).
  • math.random(n): Returns a integer in the range [1, n].
  • math.random(m, n): Returns a integer in the range [m, n].

View documents

randomseed(x : integer)

  • math.randomseed(x, y): Concatenate x and y into a 128-bit seed to reinitialize the pseudo-random generator.
  • math.randomseed(x): Equate to math.randomseed(x, 0) .
  • math.randomseed(): Generates a seed with a weak attempt for randomness.

View documents

randomstate(seed : integer?)

->(m : integer?, n : integer?) -> number

Create a new local random number state with the given optional seed value.

When no seed value is specified, the global math.randomseed value is used. When no global seed value is available, a new unique random seed is created.

Random states can be useful to create multiple, separate seeded random number generators, e.g. in pattern, gate or emit generators, which get reset with the generator functions.

examples:

return rhythm {
  emit = function(init_context)
    -- use a unique random sequence every time the rhythm gets (re)triggered
    local rand = math.randomstate(12345)
    return function(context)
      if rand(1, 10) > 5 then
        return "c5"
      else
        return "g4"
      end
  end
}

See: