Switch from std::random_shuffle to std::shuffle

std::random_shuffle is removed in C++17; it is replaced by std::shuffle,
which was introduced in C++11. The new ZcashRandomEngine class provides
the Uniform Random Number Generator interface, as inferred from
std::random_device.
This commit is contained in:
Jack Grigg 2020-09-21 23:47:42 +01:00
parent c656ae1303
commit d0cb0b74aa
2 changed files with 32 additions and 1 deletions

View File

@ -9,6 +9,7 @@
#include "uint256.h"
#include <functional>
#include <limits>
#include <stdint.h>
/**
@ -19,6 +20,34 @@ uint64_t GetRand(uint64_t nMax);
int GetRandInt(int nMax);
uint256 GetRandHash();
/**
* Implementation of a C++ Uniform Random Number Generator, backed by GetRandBytes.
*/
class ZcashRandomEngine
{
public:
typedef uint64_t result_type;
explicit ZcashRandomEngine() {}
static constexpr result_type min() {
return std::numeric_limits<result_type>::min();
}
static constexpr result_type max() {
return std::numeric_limits<result_type>::max();
}
result_type operator()() {
result_type nRand = 0;
GetRandBytes((unsigned char*)&nRand, sizeof(nRand));
return nRand;
}
double entropy() const noexcept {
return 0;
}
};
/**
* Identity function for MappedShuffle, so that elements retain their original order.
*/

View File

@ -17,6 +17,7 @@
#include "main.h"
#include "net.h"
#include "policy/policy.h"
#include "random.h"
#include "rpc/protocol.h"
#include "rpc/server.h"
#include "script/script.h"
@ -27,6 +28,7 @@
#include "crypter.h"
#include "wallet/asyncrpcoperation_saplingmigration.h"
#include <algorithm>
#include <assert.h>
#include <boost/algorithm/string/replace.hpp>
@ -3306,7 +3308,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int
vector<pair<CAmount, pair<const CWalletTx*,unsigned int> > > vValue;
CAmount nTotalLower = 0;
random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
std::shuffle(vCoins.begin(), vCoins.end(), ZcashRandomEngine());
BOOST_FOREACH(const COutput &output, vCoins)
{