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

Use 64-bit RNG for types that are 64 bits #865

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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