If using std::random_device fails, fall back to just time(nullptr)
For instance, if using LibreOfficeKit in a chroot jail, there might not be a /dev/urandom, which causes the std::random_device ctor to throw a std::runtime_error exception, at least in the libstdc++ I have. Change-Id: Icc91a4ddf92ce66c66b6ffb8b4d1a02ab5f29ee9
This commit is contained in:
@@ -12,9 +12,11 @@
|
|||||||
|
|
||||||
#include <comphelper/random.hxx>
|
#include <comphelper/random.hxx>
|
||||||
#include <rtl/instance.hxx>
|
#include <rtl/instance.hxx>
|
||||||
|
#include <rtl/ustring.hxx>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
// this is nothing but a simple wrapper around
|
// this is nothing but a simple wrapper around
|
||||||
// the std::random generators
|
// the std::random generators
|
||||||
@@ -37,13 +39,21 @@ struct RandomNumberGenerator
|
|||||||
STD_RNG_ALGO global_rng;
|
STD_RNG_ALGO global_rng;
|
||||||
RandomNumberGenerator()
|
RandomNumberGenerator()
|
||||||
{
|
{
|
||||||
std::random_device rd;
|
try
|
||||||
// initialises the state of the global random number generator
|
{
|
||||||
// should only be called once.
|
std::random_device rd;
|
||||||
// (note, a few std::variate_generator<> (like normal) have their
|
// initialises the state of the global random number generator
|
||||||
// own state which would need a reset as well to guarantee identical
|
// should only be called once.
|
||||||
// sequence of numbers, e.g. via myrand.distribution().reset())
|
// (note, a few std::variate_generator<> (like normal) have their
|
||||||
global_rng.seed(rd() ^ time(nullptr));
|
// own state which would need a reset as well to guarantee identical
|
||||||
|
// sequence of numbers, e.g. via myrand.distribution().reset())
|
||||||
|
global_rng.seed(rd() ^ time(nullptr));
|
||||||
|
}
|
||||||
|
catch (std::runtime_error& e)
|
||||||
|
{
|
||||||
|
SAL_WARN("comphelper.random", OUString("Using std::random_device failed: ") << e.what());
|
||||||
|
global_rng.seed(time(nullptr));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user