所以我正在尝试创建一些东西,并且我到处寻找生成随机数的方法。然而,无论我在哪里测试代码,它都会产生非随机数。这是我写的一个例子。
local lowdrops = {"Wooden Sword","Wooden Bow","Ion Thruster Machine Gun Blaster"}
local meddrops = {}
local highdrops = {}
function randomLoot(lootCategory)
if lootCategory == low then
print(lowdrops[math.random(3)])
end
if lootCategory == medium then
end
if lootCategory == high then
end
end
randomLoot(low)
无论我在哪里测试代码,都会得到相同的结果。例如当我在这里测试代码时http://www.lua.org/cgi-bin/demo http://www.lua.org/cgi-bin/demo它总是以“离子推进器机枪冲击波”结束,并且不随机。就此而言,只需进行测试
random = math.random (10)
print(random)
给了我 9,我有什么遗漏的吗?
你需要跑math.randomseed()
使用前一次math.random()
, 像这样:
math.randomseed(os.time())
一个可能的问题是,在某些平台上第一个数字可能不是那么“随机”。因此,更好的解决方案是在实际使用它们之前弹出一些随机数:
math.randomseed(os.time())
math.random(); math.random(); math.random()
参考:Lua数学库 http://lua-users.org/wiki/MathLibraryTutorial
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)