我最近在C#中設計了一個戰利品表,它工作得非常出色。然而,我想添加最後一件東西,這是一種計算從表格中挑選物品的概率的方法。由於我已將所有數據輸入到在線交互式圖表中,我已經知道百分比概率應該是多少,我只是不知道如何手動計算它。這裏是我LootTable類:從戰利品表中計算概率
using System;
using System.Collections.Generic;
using System.Linq;
namespace NovaEngineFramework.Framework.Probability
{
public class LootTable
{
private readonly List<string> _lootTable;
private readonly Dictionary<string , uint> _cachedLoot;
private readonly Random _rngInstance;
private bool _isRebuildRequired;
public LootTable()
{
_rngInstance = new Random();
_cachedLoot = new Dictionary<string , uint>();
_lootTable = new List<string>();
}
public LootTable(Random random)
{
this._rngInstance = random;
_cachedLoot = new Dictionary<string , uint>();
_lootTable = new List<string>();
}
public void AddLoot(uint probability , string name)
{
if(!_cachedLoot.ContainsKey(name))
{
this._cachedLoot.Add(name , probability);
_isRebuildRequired = true;
}
else
{
throw new Exception("Item: " + name + " Already Exists!");
}
}
public void DeleteLoot(string name)
{
if(_cachedLoot.ContainsKey(name))
{
this._cachedLoot.Remove(name);
_isRebuildRequired = true;
}
else
{
throw new Exception("Item: " + name + " Did not exist!");
}
}
public double CalculateProbability(string name)
{
if(_cachedLoot.ContainsKey(name))
{
return 0; // Calculate the actual percent probability here
}
throw new Exception("Item: " + name + " Does not exist.");
}
public uint CheckRarity(string name)
{
if(_cachedLoot.ContainsKey(name))
{
return _cachedLoot[ name ];
}
throw new Exception("Item: " + name + " Does not exist.");
}
public List<string> CheckLoot()
{
return this._cachedLoot.Keys.ToList();
}
public void EditLoot(string name , uint newProbability)
{
if(_cachedLoot.ContainsKey(name))
{
this._cachedLoot[ name ] = newProbability;
_isRebuildRequired = true;
}
else
{
throw new Exception("Item: " + name + " Does not exist.");
}
}
public void ClearAllLoot()
{
this._cachedLoot.Clear();
this._isRebuildRequired = true;
}
private void Build()
{
_lootTable.Clear();
foreach(KeyValuePair<string , uint> pair in _cachedLoot)
{
for(int i = 0; i < pair.Value; i++)
{
_lootTable.Add(pair.Key);
}
}
_isRebuildRequired = false;
}
public string NextRandomItem()
{
if(!_isRebuildRequired)
{
return _lootTable[ _rngInstance.Next(_lootTable.Count) ];
}
this.Build(); // A rebuild is needed, let's go ahead and take care of it!
return _lootTable[ _rngInstance.Next(_lootTable.Count) ];
}
}
}
,這裏是我的測試中執行:
LootTable swordTable = new LootTable();
swordTable.AddLoot(1 , "Sword_Legendary_SwordOfIllimitableDemise");
swordTable.AddLoot(200 , "Sword_Common_SteelGreatSword");
swordTable.AddLoot(50 , "Sword_Rare_MagicImbuedLongBlade");
swordTable.AddLoot(15 , "Sword_Epic_Hopesfire");
swordTable.AddLoot(400 , "Sword_Trash_RustySword");
Console.WriteLine(swordTable.NextRandomItem());
和圖像,以幫助促進更好的理解:
這裏是交互式的圖表鏈接,如果蒙上陰影,顯示實際的基於百分比的概率。 Interactive Chart
我要求輸出的視覺舉例:
什麼是計算戰利品表的probabilites像在線圖表不正確的方法是什麼?
最後編輯 對於那些誰想要的最終產品與包括答案,這裏是類的引擎收錄鏈接:
生鏽的劍== 400;項目總數== 666; (400/666)= 60.06% – Plutonix
@Putonix如果有人想將實際輸出從「0.600600600600601」轉換爲更具視覺吸引力的「60.06」? – Krythic
乘以10000,轉換爲int,然後除以100. –