天天看點

c語言輸出随機數switch,在Switch語句案例中使用随機數

我遇到某個代碼段的問題,該代碼段應該根據預設的難度級别向敵人提供一組随機的項目。我正在使用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