游戏概率算法的诀窍

刚从多年企业应用开发转向网页游戏开发,发现自己对这一领域很陌生。

今天写了一个从多条记录根据一定的概率抽取4个不同对象的功能,(只是算法中的一小部分)。概率值越大则对象的位置越后,发现了如果这个概率值落到了最后一个对象的位置,而不幸最后那个对象已经抽取了一次,这个时候就会出现问题了最后那个对象虽然落在抽取的区域不过不能被抽2次,因为只能抽取4次。这个时候咋办呢?

  解决的方法是在最大值的后面加多4条记录这样就不会说抽不到对象了。

 

代码是:

 INSERT INTO @PlayerTempletTempWeight select PlayerTempletID,Reactivity, HireLvl from PlayerTemplet where HireLvl <= @UserLVL order by PlayerTempletID INSERT INTO @PlayerWeightTableTemp select PlayerTempletID , Weight = (select sum(Reactivity) from @PlayerTempletTempWeight b where b.PlayerTempletID <= a.PlayerTempletID and HireLvl <= @UserLVL ), HireLvl from @PlayerTempletTempWeight a where HireLvl <= @UserLVL order by a.PlayerTempletID asc --INSERT INTO @PlayerWeightTableTemp select PlayerTempletID , Weight = (select sum(Reactivity) from PlayerTemplet b where b.PlayerTempletID<= a.PlayerTempletID and HireLvl <= @UserLVL order by a.PlayerTempletID ), HireLvl from PlayerTemplet a where HireLvl <= @UserLVL order by a.PlayerTempletID select @WeightTotal = max(weight)+1 from @PlayerWeightTableTemp where HireLvl <= @UserLVL INSERT INTO @PlayerWeightTableTemp select top 4 PlayerTempletID, Weight = @WeightTotal, HireLvl from @PlayerWeightTableTemp order by PlayerTempletID asc SET @RandWeight = Convert(int,(rand()*@WeightTotal)) INSERT INTO @PlayerTempletTableTemp SELECT TOP 1 PT.PlayerTempletID,0 FROM @PlayerWeightTableTemp PT WHERE HireLvl <= @UserLVL AND Weight > @RandWeight order by Weight asc SET @RandWeight = Convert(int,(rand()*@WeightTotal)) INSERT INTO @PlayerTempletTableTemp SELECT TOP 1 PT.PlayerTempletID,0 FROM @PlayerWeightTableTemp PT WHERE HireLvl <= @UserLVL AND Weight > @RandWeight AND PlayerTempletID NOT IN( SELECT PlayerTempletID FROM @PlayerTempletTableTemp) order by Weight asc SET @RandWeight = Convert(int,(rand()*@WeightTotal)) INSERT INTO @PlayerTempletTableTemp SELECT TOP 1 PT.PlayerTempletID,0 FROM @PlayerWeightTableTemp PT WHERE HireLvl <= @UserLVL AND Weight > @RandWeight AND PlayerTempletID NOT IN( SELECT PlayerTempletID FROM @PlayerTempletTableTemp) order by Weight asc SET @RandWeight = Convert(int,(rand()*@WeightTotal)) INSERT INTO @PlayerTempletTableTemp SELECT TOP 1 PT.PlayerTempletID,0 FROM @PlayerWeightTableTemp PT WHERE HireLvl <= @UserLVL AND Weight > @RandWeight AND PlayerTempletID NOT IN( SELECT PlayerTempletID FROM @PlayerTempletTableTemp) order by Weight asc

 

你可能感兴趣的:(游戏知识)