diff --git a/src/types/redis_hash.cc b/src/types/redis_hash.cc index 0ee9c16e3d2..d7a1ad8b7bc 100644 --- a/src/types/redis_hash.cc +++ b/src/types/redis_hash.cc @@ -405,8 +405,7 @@ rocksdb::Status Hash::RandField(const Slice &user_key, int64_t command_count, st field_values->reserve(std::min(size, count)); if (!unique || count == 1) { // Case 1: Negative count, randomly select elements or without parameter - std::random_device rd; - std::mt19937 gen(rd()); + std::mt19937 gen(std::random_device{}()); std::uniform_int_distribution dis(0, size - 1); for (uint64_t i = 0; i < count; i++) { uint64_t index = dis(gen); @@ -421,8 +420,8 @@ rocksdb::Status Hash::RandField(const Slice &user_key, int64_t command_count, st // Case 3: Requested count is less than the number of elements inside the hash std::vector indices(size); std::iota(indices.begin(), indices.end(), 0); - std::shuffle(indices.begin(), indices.end(), - std::random_device{}); // use Fisher-Yates shuffle algorithm to randomize the order + std::mt19937 gen(std::random_device{}()); + std::shuffle(indices.begin(), indices.end(), gen); // use Fisher-Yates shuffle algorithm to randomize the order for (uint64_t i = 0; i < count; i++) { uint64_t index = indices[i]; append_field_with_index(index); diff --git a/src/types/redis_zset.cc b/src/types/redis_zset.cc index 57c154acc8d..9215d6211f5 100644 --- a/src/types/redis_zset.cc +++ b/src/types/redis_zset.cc @@ -897,13 +897,14 @@ rocksdb::Status ZSet::RandMember(const Slice &user_key, int64_t command_count, std::string ns_key = AppendNamespacePrefix(user_key); ZSetMetadata metadata(false); rocksdb::Status s = GetMetadata(ns_key, &metadata); - if (!s.ok() || metadata.size == 0) return s; + if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s; + if (metadata.size == 0) return rocksdb::Status::OK(); std::vector samples; s = GetAllMemberScores(user_key, &samples); if (!s.ok() || samples.empty()) return s; - auto size = static_cast(samples.size()); + uint64_t size = samples.size(); member_scores->reserve(std::min(size, count)); if (!unique || count == 1) { @@ -915,15 +916,15 @@ rocksdb::Status ZSet::RandMember(const Slice &user_key, int64_t command_count, } } else if (size <= count) { for (auto &sample : samples) { - member_scores->push_back(sample); + member_scores->push_back(std::move(sample)); } } else { // first shuffle the samples - std::shuffle(samples.begin(), samples.end(), std::random_device{}); - + std::mt19937 gen(std::random_device{}()); + std::shuffle(samples.begin(), samples.end(), gen); // then pick the first `count` ones. for (uint64_t i = 0; i < count; i++) { - member_scores->emplace_back(samples[i]); + member_scores->emplace_back(std::move(samples[i])); } }