强化算法

--概率算法
local randlist = {85,60,55,50,50,40,35,25,20,15,12,10,10,5,5}	--概率
local countlist = {0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0}	--统计强化次数
local randlist2 = {}											--正常概率
local randlist3 = {}											--+15%概率
local randlib = 100
local target = 8
local maxfor = 100000
math.randomseed(os.time())
--初始化正常概率
for k,v in pairs(randlist)do
	local numList = {}
	local count = 0
	for i=1,randlib do
		if(count < v )then
			count = count + 1
			table.insert(numList,1)
		else
			table.insert(numList,0)
		end
	end
	--洗牌交换
	for i=1,randlib do
		local nRand = math.random(1,100)
		local a = numList[i]
		numList[i] = numList[nRand]
		numList[nRand] = a
	end
	local strList = ""
	for i,j in pairs(numList)do
		strList = strList..tostring(j)
	end
	--print("强化"..tostring(k))
	--print(strList)
	table.insert(randlist2,numList)
end
--初始化+15概率
for k,v in pairs(randlist)do
	local numList = {}
	local count = 0
	for i=1,randlib do
		if(count < v+15)then
			count = count + 1
			table.insert(numList,1)
		else
			table.insert(numList,0)
		end
	end
	--洗牌交换
	for i,j in pairs(numList)do
		local nRand = math.random(1,randlib)
		local a = numList[i]
		numList[i] = numList[nRand]
		numList[nRand] = a
	end
	local strList = ""
	for i,j in pairs(numList)do
		strList = strList..tostring(j)
	end
	--print("强化"..tostring(k))
	--print(strList)
	table.insert(randlist3,numList)
end


--完毕
local strengh = 0
local money = 0
local strmoney = ""
for i=1, maxfor do
	money = money + 1
	local index = strengh + 1
	local index2 = countlist[index]
	if(randlist2[index][index2] == 1)then
		strengh = strengh +1
	else
		strengh = strengh - 1
		if(strengh < 1)then
			strengh = 1
		end
	end
	countlist[index] = countlist[index] + 1
	if(randlib < countlist[index])then
		countlist[index] = 1
	end
	--strmoney = strmoney..tostring(strengh)
	if(target<=strengh)then
		break
	end
end
--print(strmoney)
print(money)
--吉星
money = 0
strengh = 0
strmoney = ""
for i=1, maxfor do
	money = money + 1
	local index = strengh + 1
	local index2 = countlist[index]
	if(randlist3[index][index2] == 1)then
		strengh = strengh +1
	else
		strengh = 1
		if(strengh < 1)then
			strengh = 1
		end
	end
	countlist[index] = countlist[index] + 1
	if(randlib < countlist[index])then
		countlist[index] = 1
	end
	--strmoney = strmoney..tostring(strengh)
	if(target<=strengh)then
		break
	end
end
--print(strmoney)
print(money)

  

posted @ 2018-12-01 11:30  byfei  阅读(8)  评论(0)    收藏  举报