) 아래 글 보고 cppreference를 봤는데, std::sample의 description은 다음과 같습니다. (https://en.cppreference.com/w/cpp/algorithm/sample)
Selects n elements from the sequence [first; last) (without replacement) such that each possible sample has equal probability of appearance, and writes those selected elements into the output iterator out. Random numbers are generated using the random number generator g.
즉, range 내 원소들을 한 번 꺼내면 다시 집어넣지 않는 방식으로 샘플링되게 하기 위해 reservoir sampling을 쓴 겁니다.
간단히 데모 코드를 짜 보았습니다.
결과는 다음과 같습니다. (https://wandbox.org/permlink/qXjnSrhOkBliQasV)
Map1
1 1
2 1
3 3
4 1
5 1
Map2
1 14307
2 14304
3 42824
4 14224
5 14341
즉 n = 10만을 줘도 std::sample을 쓴 map1에서는 정확히 전체 원소 개수만큼 샘플링 됩니다.
원소들이 들어있는 주머니에서 원소들을 꺼내는 (꺼낸 원소는 집어넣지 않는) 방식의 샘플링인 셈입니다.
map2처럼 reservoir sampling이 아닌 방식으로 n >> length나 n << length만큼 샘플링해 쓰고 싶으면 애초에 std::sample을 쓰면 안 됩니다!
주의하지 않고 쓰면 혼란스럽긴 하지만, 함수가 만들어진 목적이 다르다고 보면 될 것 같습니다.
댓글 0