| 359 | |
| 360 | private: |
| 361 | inline int sample(const vector<float>& v) { |
| 362 | float p = (float)rand() / (float) RAND_MAX; |
| 363 | float cumul = 0.f; |
| 364 | int idx = 0; |
| 365 | while (idx < static_cast<int>(v.size()) && p > cumul) { |
| 366 | cumul += v[idx]; |
| 367 | idx += 1; |
| 368 | } |
| 369 | return idx ? idx - 1 : 0; |
| 370 | } |
| 371 | }; |