Skip to content

Commit

Permalink
std::random_shuffle > fisher-yates
Browse files Browse the repository at this point in the history
  • Loading branch information
jwijffels committed Feb 22, 2024
1 parent 497b822 commit 54c63ff
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Starspace/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,16 @@ Real EmbedModel::train(shared_ptr<InternDataHandler> data,
int i = 0;
for (auto& idx: indices) idx = i++;
}
//std::random_shuffle(indices.begin(), indices.end(), randWrapper);
Rcpp::IntegerVector indices_r = Rcpp::sample(numSamples, numSamples, false, R_NilValue, true) - 1;
for (auto ii = 0; ii < numSamples; ii++) {
indices[ii] = indices_r[ii];
// std::random_shuffle(indices.begin(), indices.end(), randWrapper);
// Rcpp::IntegerVector indices_r = Rcpp::sample(numSamples, numSamples, false, R_NilValue, true) - 1;
// for (auto ii = 0; ii < numSamples; ii++) {
// indices[ii] = indices_r[ii];
// }
// Fisher-Yates Shuffle Algorithm - https://gallery.rcpp.org/articles/stl-random-shuffle/
int j;
for (int i = 0; i < numSamples - 1; i++) {
j = i + randWrapper(n - i);
std::swap(indices[i], indices[j]);
}

// Compute word negatives
Expand Down

0 comments on commit 54c63ff

Please sign in to comment.