mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-06-04 18:54:00 +08:00
Fix internal::random(x,y) for integer types. The previous implementation could return y+1. The new implementation uses rejection sampling to get an unbiased behabior.
This commit is contained in:
parent
8580eb6808
commit
d99ab35f9e
@ -526,7 +526,24 @@ struct random_default_impl<Scalar, false, true>
|
|||||||
|
|
||||||
static inline Scalar run(const Scalar& x, const Scalar& y)
|
static inline Scalar run(const Scalar& x, const Scalar& y)
|
||||||
{
|
{
|
||||||
return x + Scalar((NonInteger(y)-x+1) * std::rand() / (RAND_MAX + NonInteger(1)));
|
using std::max;
|
||||||
|
Scalar range = (max)(Scalar(0),Scalar(y-x));
|
||||||
|
Scalar offset = 0;
|
||||||
|
if(range<=RAND_MAX)
|
||||||
|
{
|
||||||
|
// rejection sampling
|
||||||
|
int divisor = RAND_MAX/(range+1);
|
||||||
|
|
||||||
|
do {
|
||||||
|
offset = Scalar(std::rand() / divisor);
|
||||||
|
} while (offset > range);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
offset = std::rand() * range;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x + offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline Scalar run()
|
static inline Scalar run()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user