title: Generating uniform random numbers in Lua
body: <p>I am working on programming a Markov chain in Lua, and one element of this requires me to uniformly generate random numbers. Here is a simplified example to illustrate my question:</p>
  
  <pre><code>example = function(x)
      local r = math.random(1,10)
      print(r)
      return x[r]
  end
  
  exampleArray = {"a","b","c","d","e","f","g","h","i","j"}
  
  print(example(exampleArray))
  </code></pre>
  
  <p>My issue is that when I re-run this program multiple times (mash <kbd>F5</kbd>) the exact same random number is generated resulting in the example function selecting the exact same array element. However, if I include many calls to the example function within the single program by repeating the print line at the end many times I get suitable random results. </p>
  
  <p>This is not my intention as a proper Markov pseudo-random text generator should be able to run the same program with the same inputs multiple times and output different pseudo-random text every time. I have tried resetting the seed using <code>math.randomseed(os.time())</code> and this makes it so the random number distribution is no longer uniform. My goal is to be able to re-run the above program and receive a randomly selected number every time.</p>