bab2min / EigenRand

Fastest Random Distribution Generator for Eigen
https://bab2min.github.io/eigenrand/
MIT License
92 stars 13 forks source link

uniformIntLike creates values outside [min,max] #42

Closed mauzigoe closed 2 years ago

mauzigoe commented 2 years ago

EigenRand Version: 0.4.0

When trying to create uniform random values between -1 and 1 with Eigen::Rand::uniformIntLike I get also values equal 2.

I used the following code to create this bug:

#include <iostream>
#include <eigen3/Eigen/Dense>
#include <EigenRand/EigenRand>

int main(){
  uint64_t seed = std::random_device{}();
  Eigen::Rand::P8_mt19937_64 generator{seed};

  Eigen::VectorXi p;
  p.resize(2);

  for (uint i = 0; i<100; i++){
    auto x = Eigen::Rand::uniformIntLike(p, generator,-1,1);
    std::cout << "x :" << x << std::endl;

  }
}

In the output I one should see that there are values equal 2 though the range of the distribution is defined to be [-1,1].

For the range [0,1] the function works as described in the documentation.

bab2min commented 2 years ago

Hi @mauzigoe Thank you for reporting the bug. It has been confirmed that there is a bug in scalar generator of UniformInt class.

https://github.com/bab2min/EigenRand/blob/be563c3abc65864e8c8c70a444a374bbb9b70825/EigenRand/Dists/Discrete.h#L251-L280 Line 267 should return cands + pmin, not cands. I'll fix this bug in the next update soon.

Thank you again for reporting.

mauzigoe commented 2 years ago

Cool. Thank you for your work.