@@ -344,7 +344,7 @@ void AIPlayerJH::PlanNewBuildings(const unsigned gf)
344344 DistributeGoodsByBlocking (GoodType::Boards, 30 );
345345 DistributeGoodsByBlocking (GoodType::Stones, 50 );
346346 // go to the picked random warehouse and try to build around it
347- int randomStore = AI ::randomValue<int >(0 , storehouses.size ());
347+ int randomStore = AI ::randomValue<int >(0 , storehouses.size () - 1 );
348348 auto it = storehouses.begin ();
349349 std::advance (it, randomStore);
350350 const MapPoint whPos = (*it)->GetPos ();
@@ -365,7 +365,7 @@ void AIPlayerJH::PlanNewBuildings(const unsigned gf)
365365 const std::list<nobMilitary*>& militaryBuildings = aii.GetMilitaryBuildings ();
366366 if (militaryBuildings.empty ())
367367 return ;
368- int randomMiliBld = AI ::randomValue<int >(0 , militaryBuildings.size ());
368+ int randomMiliBld = AI ::randomValue<int >(0 , militaryBuildings.size () - 1 );
369369 auto it2 = militaryBuildings.begin ();
370370 std::advance (it2, randomMiliBld);
371371 MapPoint bldPos = (*it2)->GetPos ();
@@ -1209,7 +1209,7 @@ void AIPlayerJH::HandleExpedition(const noShip* ship)
12091209 aii.FoundColony (ship);
12101210 else
12111211 {
1212- const unsigned offset = AI ::randomValue<unsigned >(0 , helpers::MaxEnumValue_v<ShipDirection>);
1212+ const unsigned offset = AI ::randomValue<unsigned >(0 , helpers::MaxEnumValue_v<ShipDirection> - 1 );
12131213 for (auto dir : helpers::EnumRange<ShipDirection>{})
12141214 {
12151215 dir = ShipDirection ((rttr::enum_cast (dir) + offset) % helpers::MaxEnumValue_v<ShipDirection>);
@@ -1254,7 +1254,7 @@ void AIPlayerJH::HandleTreeChopped(const MapPoint pt)
12541254
12551255 UpdateNodesAround (pt, 3 );
12561256
1257- if (AI ::randomValue<int >(0 , 2 ) == 0 )
1257+ if (AI ::randomValue<int >(0 , 1 ) == 0 )
12581258 AddMilitaryBuildJob (pt);
12591259 else // if (random % 12 == 0)
12601260 AddBuildJob (BuildingType::Woodcutter, pt);
@@ -1536,7 +1536,7 @@ void AIPlayerJH::TryToAttack()
15361536 // We skip the current building with a probability of limit/numMilBlds
15371537 // -> For twice the number of blds as the limit we will most likely skip every 2nd building
15381538 // This way we check roughly (at most) limit buildings but avoid any preference for one building over an other
1539- if (AI ::randomValue<unsigned >(0 , numMilBlds) > limit)
1539+ if (AI ::randomValue<unsigned >(0 , numMilBlds - 1 ) > limit)
15401540 continue ;
15411541
15421542 if (milBld->GetFrontierDistance () == FrontierDistance::Far) // inland building? -> skip it
@@ -1569,7 +1569,7 @@ void AIPlayerJH::TryToAttack()
15691569
15701570 // shuffle everything but headquarters and harbors without any troops in them
15711571 std::shuffle (potentialTargets.begin () + hq_or_harbor_without_soldiers, potentialTargets.end (),
1572- std::mt19937 (AI ::randomValue<unsigned >(0 , 2048 )));
1572+ std::mt19937 (AI ::randomValue<unsigned >(0 , 2047 )));
15731573
15741574 // check for each potential attacking target the number of available attacking soldiers
15751575 for (const nobBaseMilitary* target : potentialTargets)
@@ -1703,7 +1703,7 @@ void AIPlayerJH::TrySeaAttack()
17031703 unsigned skip = 0 ;
17041704 if (searcharoundharborspots.size () > 15 )
17051705 skip =
1706- std::max<int >(AI ::randomValue<int >(0 , static_cast <int >(searcharoundharborspots.size () / 15 + 1 ) * 15 ), 1 ) - 1 ;
1706+ std::max<int >(AI ::randomValue<int >(0 , static_cast <int >(searcharoundharborspots.size () / 15 ) * 15 ), 1 ) - 1 ;
17071707 for (unsigned i = skip; i < searcharoundharborspots.size () && limit > 0 ; i++)
17081708 {
17091709 limit--;
0 commit comments