EigenRand  0.2.0
Getting Started

Installation

You can install EigenRand by just downloading the source codes from the repository. Since EigenRand is a header-only library like Eigen, none of binaries needs to be installed. All you need is Eigen 3.3.7 and C++11 compiler.

Simple Random Matrix Generators

#include <iostream>
#include <Eigen/Dense>
#include <EigenRand/EigenRand>
using namespace Eigen;
int main()
{
// Initialize random number generator with seed=42 for following codes.
// Or you can use C++11 RNG such as std::mt19937 or std::ranlux48.
Rand::Vmt19937_64 urng{ 42 };
// this will generate 4x4 real matrix with range [-1, 1]
MatrixXf mat = Rand::balanced<MatrixXf>(4, 4, urng);
std::cout << mat << std::endl;
// this will generate 10x10 real 2d array on the normal distribution
ArrayXXf arr = Rand::normal<ArrayXXf>(10, 10, urng);
std::cout << arr << std::endl;
return 0;
}

Random Matrix Functions with suffix '-Like'

Basically, in order to call each random distribution function of EigenRand, template parameters must be passed following the dense matrix or array type to be created. But, if you have an instance of Eigen::Matrix or Eigen::Array already, you can use -Like function to generate a random matrix or array with the same type and shape.

#include <iostream>
#include <Eigen/Dense>
#include <EigenRand/EigenRand>
using namespace Eigen;
int main()
{
Rand::Vmt19937_64 urng{ 42 };
MatrixXf mat{ 10, 10 };
// this will generate a random matrix in MatrixXf type with the shape (10, 10)
// note: it doesn't change mat at all.
Rand::balancedLike(mat, urng);
// if you want to assign a random matrix into itself, use assignment operator.
mat = Rand::balancedLike(mat, urng);
std::cout << mat << std::endl;
return 0;
}

Every random distribution function has its corresponding -Like function.

Eigen::Rand::Vmt19937_64
std::mt19937_64 Vmt19937_64
same as std::mt19937_64 when EIGEN_DONT_VECTORIZE, Pmt19937_64<internal::Packet4i> when SSE2 enabled ...
Definition: PacketRandomEngine.h:551
Eigen::Rand::balancedLike
const BalancedType< Derived, Urng > balancedLike(const Derived &o, Urng &&urng)
generates reals in a range [-1, 1]
Definition: Core.h:145