Fix random numbers on linux wheel build (#5975)

This commit is contained in:
Philipp Moritz
2019-10-22 17:52:12 -07:00
committed by GitHub
parent f7bda0abad
commit b6e7ed20ce
2 changed files with 6 additions and 5 deletions
+1 -1
View File
@@ -595,7 +595,7 @@ cc_library(
deps = [
":sha256",
"@com_github_google_glog//:glog",
"@com_google_absl//absl/random",
"@com_google_absl//absl/time",
"@plasma//:plasma_client",
],
)
+5 -4
View File
@@ -1,8 +1,9 @@
#ifndef RAY_UTIL_SAMPLE_H
#define RAY_UTIL_SAMPLE_H
#include "absl/random/random.h"
#include "absl/random/uniform_int_distribution.h"
#include <random>
#include "absl/time/clock.h"
// Randomly samples num_elements from the elements between first and last using reservoir
// sampling.
@@ -10,17 +11,17 @@ template <class Iterator, class T = typename std::iterator_traits<Iterator>::val
void random_sample(Iterator begin, Iterator end, size_t num_elements,
std::vector<T> *out) {
out->resize(0);
absl::BitGen gen;
if (num_elements == 0) {
return;
}
std::default_random_engine gen(absl::GetCurrentTimeNanos());
size_t current_index = 0;
for (auto it = begin; it != end; it++) {
if (current_index < num_elements) {
out->push_back(*it);
} else {
size_t random_index = absl::uniform_int_distribution<size_t>(0, current_index)(gen);
size_t random_index = std::uniform_int_distribution<size_t>(0, current_index)(gen);
if (random_index < num_elements) {
out->at(random_index) = *it;
}