Lua math.random not working -


so i'm trying create little , have looked on place looking ways of generating random number. no matter test code, results in non-random number. here example wrote up.

local lowdrops =  {"wooden sword","wooden bow","ion thruster machine gun blaster"} local meddrops =  {} local highdrops = {}  function randomloot(lootcategory)     if lootcategory == low         print(lowdrops[math.random(3)])     end     if lootcategory == medium      end     if lootcategory == high      end end  randomloot(low) 

wherever test code same result. example when test code here http://www.lua.org/cgi-bin/demo ends "ion thruster machine gun blaster" , doesen't randomize. matter testing simply

random = math.random (10) print(random) 

gives me 9, there i'm missing?

you need run math.randomseed() once before using math.random(), this:

math.randomseed(os.time()) 

one possible problem first number may not "randomized" in platforms. better solution pop random number before using them real:

math.randomseed(os.time()) math.random(); math.random(); math.random() 

reference: lua math library


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -