src/rand.cpp
/**
 * Returns a random sample of sample_size numbers from 0 to n-1.
 * TODO this is inefficient - complexity is O(n) instead of O(sample_size)
 */
Returns a random sample of sample_size numbers from 0 to n-1.
TODO this is inefficient - complexity is O(n) instead of O(sample_size)
Returns a random sample of sample_size numbers from 0 to n - 1 . TODO this is inefficient - complexity is O ( n ) instead of O ( sample_size )
std::vector<uint32_t> random_sample(uint32_t n, uint32_t sample_size) {
    if (sample_size > n) {
        sample_size = n;
    }
    vector<uint32_t> sample(sample_size);
    for (auto i = 0u; i < sample_size; ++i) {
        sample[i] = i;
    }
    for (auto i = sample_size; i < n; ++i) {
        std::uniform_int_distribution<uint32_t> dist{0, i};
        auto r = dist(get_random_engine());
        if (r < sample_size) {
            sample[r] = i;
        }
    }
    return sample;
}
std :: vector < uint32_t > random_sample ( uint32_t n , uint32_t sample_size ) { if ( sample_size > n ) { sample_size = n ; } vector < uint32_t > sample ( sample_size ) ; for ( auto i = 0u ; i < sample_size ; ++ i ) { sample [ i ] = i ; } for ( auto i = sample_size ; i < n ; ++ i ) { std :: uniform_int_distribution < uint32_t > dist { 0 , i } ; auto r = dist ( get_random_engine ( ) ) ; if ( r < sample_size ) { sample [ r ] = i ; } } return sample ; }
Returns a random sample of sample_size numbers from 0 to n-1.
Returns a random sample of sample_size numbers from 0 to n - 1 .