7#ifndef SPECTRA_SIMPLE_RANDOM_H
8#define SPECTRA_SIMPLE_RANDOM_H
30inline long next_long_rand(
long seed)
32 constexpr unsigned int m_a = 16807;
33 constexpr unsigned long m_max = 2147483647L;
37 lo = m_a * (long) (seed & 0xFFFF);
38 hi = m_a * (long) ((
unsigned long) seed >> 16);
39 lo += (hi & 0x7FFF) << 16;
56template <
typename Scalar>
59 static Scalar run(
long& seed)
61 constexpr unsigned long m_max = 2147483647L;
63 seed = next_long_rand(seed);
64 return Scalar(seed) / Scalar(m_max) - Scalar(0.5);
68template <
typename RealScalar>
69struct RandomScalar<std::complex<RealScalar>>
71 static std::complex<RealScalar> run(
long& seed)
73 RealScalar r = RandomScalar<RealScalar>::run(seed);
74 RealScalar i = RandomScalar<RealScalar>::run(seed);
75 return std::complex<RealScalar>(r, i);
80template <
typename Scalar =
double>
85 using RealScalar =
typename Eigen::NumTraits<Scalar>::Real;
86 using Index = Eigen::Index;
87 using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
92 SimpleRandom(
unsigned long init_seed)
94 constexpr unsigned long m_max = 2147483647L;
95 m_rand = init_seed ? (init_seed & m_max) : 1;
101 return RandomScalar<Scalar>::run(m_rand);
106 void random_vec(Vector& vec)
108 const Index len = vec.size();
109 for (Index i = 0; i < len; i++)
117 Vector random_vec(
const Index len)