## 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:

- Lua is tiny. As someone accostumed to Python and Ruby, it is shocking to see such a small standard library. For example, this is the manual – there are only 158 Lua functions listed there.
- The syntax is incredibly simple. Take a look at these diagrams; if you understand the Extended Backus-Naur Form, you can read Lua's syntax quite easily. For comparison, Ruby's syntax is complex enough that there are lots (and lots and lots) of small corner cases that I probably never heard about, even after years using it. Ah! And Ruby's parse.y has 11.3k lines.
- Lua was built with embedding in mind; it is used for interface customization in World of Warcraft, for example.
- It is a Brazilian programming language! :-) Lua was created in 1993 in Rio de Janeiro, according to Wikipedia.

## 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. :)