Welcome to mirror list, hosted at ThFree Co, Russian Federation.

random.cpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 964cffa314ce06cee71826ebba1011f6f0e1cd95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "base/random.hpp"

#include <algorithm>
#include <numeric>

namespace base
{
std::vector<size_t> RandomSample(size_t n, size_t k, std::minstd_rand & rng)
{
  std::vector<size_t> result(std::min(k, n));
  std::iota(result.begin(), result.end(), 0);

  for (size_t i = k; i < n; ++i)
  {
    size_t const j = rng() % (i + 1);
    if (j < k)
      result[j] = i;
  }

  return result;
}
}  // base