Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate different randomized matrices with min and max values? #24

Closed
NicoBrug opened this issue Apr 28, 2021 · 3 comments
Closed
Labels
enhancement New feature or request

Comments

@NicoBrug
Copy link

I use the library to generate randomized arrays. I had 2 questions:
Why when I do
MatrixXd m{rows,cols}; Rand::Vmt19937_64 urng{ 42 }; Rand::balancedLike(m, urng); m = Rand::balancedLike(m, urng);
it always returns the same matrix with the same values.

And my other question: is it possible to choose min and max values to generate random numbers?

@bab2min
Copy link
Owner

bab2min commented Apr 28, 2021

Hi @NicoBrug

MatrixXd m{rows,cols}; 
Rand::Vmt19937_64 urng{ 42 }; 
m = Rand::balancedLike(m, urng);

It always returns the same result because the seed of random number generator is fixed as 42.
If you want to get different results every run, you should pass the seed with different value like:

MatrixXd m{rows,cols}; 
auto seed = std::random_device{}(); // it requires #include <random>
Rand::Vmt19937_64 urng{ seed }; 
m = Rand::balancedLike(m, urng);

For your second question:
You can change the minimum and maximum values of distribution with some arithmetic:

MatrixXd m{rows,cols}; 
double min_v = -2, max_v = 3;
Rand::Vmt19937_64 urng{ 42 }; 
m = ((Rand::balancedLike(m, urng).array() + 1) * (max_v - min_v) / 2 + min_v).matrix();

It will generate a matrix with uniform random reals in [-2, 3].
But it looks ugly, so in the next update I'll add a function to set the min/max value more simply.

@bab2min bab2min added the enhancement New feature or request label Apr 28, 2021
bab2min added a commit that referenced this issue Jul 15, 2021
bab2min added a commit that referenced this issue Jul 16, 2021
@bab2min
Copy link
Owner

bab2min commented Jul 16, 2021

A new function overloading balanced and balancedLike for arbitrary range [a, b] was added at version 0.3.5.

@bab2min bab2min closed this as completed Jul 16, 2021
@andrewfowlie
Copy link

@bab2min I would find an example demonstrating the use of std::random_device helpful in the getting started section of the docs (https://bab2min.github.io/eigenrand/v0.5.0/en/getting_started.html). It's not the first time I've used C++11 style random number generation, but still somehow didn't find this obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants