diff --git a/stl/inc/random b/stl/inc/random index 09b7418d93e..77726564f94 100644 --- a/stl/inc/random +++ b/stl/inc/random @@ -490,6 +490,13 @@ _NODISCARD _Uint _Next_linear_congruential_value(_Uint _Prev) noexcept { // numeric_limits::max() plus 1." That is: Just do the multiply // and let normal unsigned modulo take care of it return static_cast<_Uint>(static_cast<_Uint>(_Ax * _Prev) + _Cx); + } else if constexpr (_Cx == 0 && _Mx == 2147483647) { + // for minstd_rand and minstd_rand0 we can improve performance by avoiding constant divisions + auto _Mul = static_cast(_Prev) * _Ax; + _Mul = (_Mul >> 31) + (_Mul & _Mx); + _Mul = _Mul < _Mx ? _Mul : _Mul - _Mx; + + return static_cast<_Uint>(_Mul); } else if constexpr (_Cx <= UINT_MAX && static_cast<_Uint>(_Mx - 1) <= (UINT_MAX - _Cx) / _Ax) { // unsigned int is sufficient to store intermediate calculation const auto _Mul =