From a2195b9b7fba5da7f47903961db3fb31f6d7146c Mon Sep 17 00:00:00 2001 From: stoffu Date: Wed, 3 Apr 2019 14:10:24 +0900 Subject: crypto: replace rand()%N idiom with unbiased rand_idx(N) --- src/wallet/wallet2.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/wallet/wallet2.cpp') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 0e82f1a91..a4b063541 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -5719,7 +5719,7 @@ namespace { CHECK_AND_ASSERT_MES(!vec.empty(), T(), "Vector must be non-empty"); - size_t idx = crypto::rand() % vec.size(); + size_t idx = crypto::rand_idx(vec.size()); return pop_index (vec, idx); } @@ -5822,7 +5822,7 @@ size_t wallet2::pop_best_value_from(const transfer_container &transfers, std::ve } else { - idx = crypto::rand() % candidates.size(); + idx = crypto::rand_idx(candidates.size()); } return pop_index (unused_indices, candidates[idx]); } @@ -7474,7 +7474,7 @@ void wallet2::get_outs(std::vector> if (n_rct == 0) return rct_offsets[block_offset] ? rct_offsets[block_offset] - 1 : 0; MDEBUG("Picking 1/" << n_rct << " in " << (last_block_offset - first_block_offset + 1) << " blocks centered around " << block_offset + rct_start_height); - return first_rct + crypto::rand() % n_rct; + return first_rct + crypto::rand_idx(n_rct); }; size_t num_selected_transfers = 0; @@ -9603,7 +9603,7 @@ std::vector wallet2::create_transactions_all(uint64_t below if (unused_transfer_dust_indices_per_subaddr.count(0) == 1 && unused_transfer_dust_indices_per_subaddr.size() > 1) unused_transfer_dust_indices_per_subaddr.erase(0); auto i = unused_transfer_dust_indices_per_subaddr.begin(); - std::advance(i, crypto::rand() % unused_transfer_dust_indices_per_subaddr.size()); + std::advance(i, crypto::rand_idx(unused_transfer_dust_indices_per_subaddr.size())); unused_transfers_indices = i->second.first; unused_dust_indices = i->second.second; LOG_PRINT_L2("Spending from subaddress index " << i->first); -- cgit v1.2.3