我遇到某个代码段的问题,该代码段应该根据预设的难度级别向敌人提供一组随机的项目。我正在使用Bjorne随机函数的修改模板版本。当我使用他的原始版本时,我仍然遇到了问题:
template
T rand_num(const T & low, const T & high)
{
static std::default_random_engine re{};
using Dist = std::uniform_int_distribution;
static Dist uid{};
return uid(re, Dist::param_type{ low,high });
}
当我通过制作一个封装EnemyAI的{{1}}对象并将难度设置为1来测试该部分时,它始终将随机数设置为1并始终选择,在这种情况下,生命药水。如果我将第二个OffensiveEntity语句设置为if条件,它将选择该棒。
if (tempRandom == 1)
这个问题的原因是什么?这是输出:
void EnemyAI::Equip()
{
m_offensiveEntity->ClearItems();
std::vector<:shared_ptr>> tempItems;
int tempRandom = 0;
switch (m_difficultyLevel)
{
case 0:
case 1:
{
tempRandom = rand_num(1, 4);
if ((tempRandom == 1) || (tempRandom == 2) || (tempRandom == 3) || (tempRandom == 4))
tempItems.push_back(CreateTempItem("Health Potion : HP", 3, 3,
-10, Result::Effect::nothing));
if (tempRandom == 3)
tempItems.push_back(CreateTempItem("Wooden Stick : DMG", 5, 2, 10, Result::Effect::nothing, 3, 13, Result::Effect::nothing));
break;
}
case 2: ... etc
输入时:
Health Potion : HP name
3 durability
-10 total damage
3 energy cost
0 effect