是否可以更改 corona SDK 中选择的概率?
Is it possible to change the probability of a choice in corona SDK?
我不确定是否可行,但我想更改 "choice" 完成的概率。我有这样的代码:
({move, bomb})[math.random(2)]
我的目标是让炸弹出现或被选中的频率低于 'move'。这可能吗?
当然可以。可以做到的一种方法是
( { move, bomb } )[ math.random( 10 ) <= 7 and 1 or 2 ]
你有 70% 选择第一个元素,30% 选择第二个元素。
如果您需要从 4 个元素中选择,您可以使用下面的代码
local mRandom = math.random
local objects = { 'object1', 'object2', 'object3', 'object4', }
local prob = { 10, 30, 60, 100 } -- 10% for object1 20%=30%-10% for object2 30%=60%-30% for object3 and 40%=100%-60% for last object.
local mychoice
math.randomseed( os.time() )
local rand = mRandom( 100 )
for i=1, #prob do
if rand <= prob[i] then
mychoice = i
break
end
end
我不确定是否可行,但我想更改 "choice" 完成的概率。我有这样的代码:
({move, bomb})[math.random(2)]
我的目标是让炸弹出现或被选中的频率低于 'move'。这可能吗?
当然可以。可以做到的一种方法是
( { move, bomb } )[ math.random( 10 ) <= 7 and 1 or 2 ]
你有 70% 选择第一个元素,30% 选择第二个元素。
如果您需要从 4 个元素中选择,您可以使用下面的代码
local mRandom = math.random
local objects = { 'object1', 'object2', 'object3', 'object4', }
local prob = { 10, 30, 60, 100 } -- 10% for object1 20%=30%-10% for object2 30%=60%-30% for object3 and 40%=100%-60% for last object.
local mychoice
math.randomseed( os.time() )
local rand = mRandom( 100 )
for i=1, #prob do
if rand <= prob[i] then
mychoice = i
break
end
end