Skip to content

Commit

Permalink
Use 64-bit RNG for types that are 64 bits (boostorg#865)
Browse files Browse the repository at this point in the history
Do this instead of casting down every time we do a normal distribution.
  • Loading branch information
AreaZR committed Jan 19, 2023
1 parent f2c4221 commit 243e3c0
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions include/boost/math/tools/random_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <vector>
#include <cstddef>
#include <random>
#include <type_traits>
#include <cstddef>
#include <vector>

namespace boost { namespace math {

Expand All @@ -25,12 +25,12 @@ std::vector<T> generate_random_vector(std::size_t size, std::size_t seed)
}
std::vector<T> v(size);

std::mt19937 gen(seed);
std::mt19937_64 gen(seed);

std::normal_distribution<T> dis(0, 1);
for(std::size_t i = 0; i < v.size(); ++i)
for (auto& x : v)
{
v[i] = dis(gen);
x = dis(gen);
}
return v;
}
Expand All @@ -49,9 +49,9 @@ std::vector<T> generate_random_uniform_vector(std::size_t size, std::size_t seed

std::uniform_real_distribution<T> dis(lower_bound, upper_bound);

for (auto& i : v)
for (auto& x : v)
{
i = dis(gen);
x = dis(gen);
}

return v;
Expand All @@ -70,9 +70,9 @@ std::vector<T> generate_random_vector(std::size_t size, std::size_t seed, T mean
std::mt19937 gen(seed);

std::normal_distribution<T> dis(mean, stddev);
for (std::size_t i = 0; i < v.size(); ++i)
for (auto& x : v)
{
v[i] = dis(gen);
x = dis(gen);
}
return v;
}
Expand All @@ -87,13 +87,13 @@ std::vector<T> generate_random_vector(std::size_t size, std::size_t seed)
}
std::vector<T> v(size);

std::mt19937 gen(seed);
std::mt19937_64 gen(seed);

// Rescaling by larger than 2 is UB!
std::uniform_int_distribution<T> dis(std::numeric_limits<T>::lowest()/2, (std::numeric_limits<T>::max)()/2);
for (std::size_t i = 0; i < v.size(); ++i)
for (auto& x : v)
{
v[i] = dis(gen);
x = dis(gen);
}
return v;
}
Expand Down

0 comments on commit 243e3c0

Please sign in to comment.