求一个语法,从几种物品中随机取一种
lua吧
全部回复
仅看楼主
level 1
sdgmlj 楼主
以下是一个游戏mod的一段语句,我想在五种物品中随机取一种,但是有时候会出现多种,请问应该怎么写?谢谢了
results =
{
{ name= "titanium-ore-s", probability = 0.2,amount= 500},
{ name= "silicon-s", probability = 0.2,amount= 500},
{ name = "raw-rare-metals-s",probability = 0.2,amount = 500 },
{ name = "combustible-ice",probability = 0.2,amount = 500 },
{ name = "amethyst",probability = 0.2,amount = 500 }
},
我本以为每一种的概率设为20%,就可以随机出一种,但是每一种是单独判断的,有时候会出现多种,请问正确应该怎么写?
2021年07月04日 02点07分 1
level 2
results[math.random(1,#results)]
[滑稽]好像是这样 你试试
2021年07月04日 16点07分 2
谢谢,能说一下具体怎么写吗?那五种材料放在哪?
2021年07月04日 22点07分
level 1
sdgmlj 楼主
谢谢,能写具体一点吗?那五种材料名字放在哪里?我是小白[乖]
2021年07月04日 21点07分 3
level 1
朋友加下QQ好 一起学习 378885008
2021年07月05日 02点07分 4
level 2
--重置随机数种子
math.randomseed(os.time())
--物品表
物品 = {"一叶草","二叶草","三叶草","四叶草","五叶草"}
--取随机数
local 随机数 = math.random(1,#物品)
for k,p in pairs(物品) do
if k == 随机数 then
print("当前随机数为:"..随机数..",物品:"..物品[随机数])
end
end
2021年07月28日 06点07分 5
请问为什么要重置随机数种子?没有那句话也能允许的呀
2021年11月01日 03点11分
运行
2021年11月01日 03点11分
1