반응형
아이템 선택
x
using namespace std;struct Table{ struct Item { int id; int weight; }; vector<Item> table;};int select(const Table& table, int w) { int randomNum = rand() % (w + 1); int sum = 0; for (auto it : table.table) { sum += it.weight; if (randomNum <= sum) { return it.id; } } return -1;//fail}vector<int> select2(const Table& table, int w, int k) { unordered_set<int> selected; while (selected.size() < k) { selected.insert(select(table, w)); } return vector<int>(selected.begin(), selected.end());}일정 확률에 따라 아이템 인덱스를 반환
반응형