Xtensor random::randn returning number number below lower bound and above upper bound

198 Views Asked by At

I am trying to generate random numbers of shape {10} and range between -0.5 and 0.5

#include <iostream>
#include <xtensor/xio.hpp>
#include <xtensor/xrandom.hpp>

int main() {
    xt::xarray<double> a = xt::random::randn<double>({10}, -0.5, 0.5);
    std::cout << a;
    return 0;
}

but it's returning an array

{-0.432735, -0.573191, -0.269675, -1.435692, -0.418144, -0.607127,
 -0.350702, -0.913972, -0.494892,  0.027733}

where -0.573191 is smaller and -1.435692 is bigger than range

1

There are 1 best solutions below

7
On BEST ANSWER

According to the documentation for randn:

template<class T, class S, class E = random::default_engine_type>
auto xt::random::randn(const S& shape, T mean = 0, T std_dev = 1,
                       E& engine = random::get_default_random_engine())

The function draws numbers from std::normal_distribution and you've set the mean value to be -0.5 and the standard deviation to be 0.5. These are not lower and upper bounds.


You may want to use the rand function instead:

template<class T, class S, class E = random::default_engine_type>
auto xt::random::rand(const S& shape, T lower = 0, T upper = 1,
                      E& engine = random::get_default_random_engine())

This draws numbers from std::uniform_real_distribution - so, it's not the same thing, but it has a lower and an upper bound.