Skip to content

Commit 497b834

Browse files
authored
Speed up random range creation (#6812)
1 parent 8989b6e commit 497b834

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

code/utils/RandomRange.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ class RandomRange {
7777
ValueType m_minValue;
7878
ValueType m_maxValue;
7979

80+
//Sampling a random_device is REALLY expensive.
81+
//Instead of sampling one for each seed, create a pseudorandom seeder which is initialized ONCE from a random_device.
82+
inline static std::mt19937 seeder {std::random_device()()};
83+
8084
public:
8185
template <typename T, typename... Ts, typename = typename std::enable_if<(sizeof... (Ts) >=1 || !std::is_convertible<T, ValueType>::value) && !std::is_same_v<std::decay_t<T>, RandomRange>, int>::type>
8286
explicit RandomRange(T&& distributionFirstParameter, Ts&&... distributionParameters)
83-
: m_generator(std::random_device()()), m_distribution(distributionFirstParameter, distributionParameters...)
87+
: m_generator(seeder()), m_distribution(distributionFirstParameter, distributionParameters...)
8488
{
8589
m_minValue = static_cast<ValueType>(m_distribution.min());
8690
m_maxValue = static_cast<ValueType>(m_distribution.max());
@@ -94,7 +98,7 @@ class RandomRange {
9498
m_constant = true;
9599
}
96100

97-
RandomRange() : m_generator(std::random_device()()), m_distribution()
101+
RandomRange() : m_generator(seeder()), m_distribution()
98102
{
99103
m_minValue = static_cast<ValueType>(0.0);
100104
m_maxValue = static_cast<ValueType>(0.0);

0 commit comments

Comments
 (0)