Playing with Lua

I work for a mobile games company as a data scientist. I use Ruby for data wrangling and some sorts of analysis, Python for more specific things (essentially scikit-learn) and bash scripts for gluing everything together.

The developers use Corona for creating our games, which uses Lua. I decided to give that language a try.

Some facts:

Random number generators

After finding so many interesting features about the language, I wrote some random number generators:

-- Some RNGs for getting to play with Lua.
--
-- Carlos Agarie <carlos@onox.com.br>
--
-- Public domain.

-- N(mean; std^2).
function gauss(mean, std)
  if std <= 0.0 then error("standard deviation must be positive!") end

  u1 = math.random()
  u2 = math.random()

  r = math.sqrt(-2.0 * math.log(u1))
  theta = 2.0 * math.pi * u2
  return mean + std * r * math.sin(theta)
end

-- This distribution models the time between events in a Poisson process.
function exponential(mean)
  if mean <= 0.0 then error("mean must be positive!") end

  return -mean * math.log(math.random())
end

-- This is a non-exponential type of distribution, one without a mean value.
function cauchy(median, scale)
  if scale <= 0.0 then error("scale must be positive!") end

  return median + scale * math.tan(math.pi * (math.random() - 0.5))
end

I decided to write RNGs after reading John D. Cook’s post about RNGs in Julia. :)