GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
libstdc++

Do Not Assume URBG::result_type Exists

The `ranges::sample` and `ranges::shuffle` algorithms now work with random number generators that don't define `result_type`, improving compatibility.

This commit fixes an issue in the ranges::sample and ranges::shuffle algorithms in libstdc++. These algorithms are designed to work with types that model std::uniform_random_bit_generator. The original implementation incorrectly assumed that all such generators would have a nested result_type member. This commit changes the algorithms to use decltype(__g()) instead of result_type to determine the return type of the generator, making them compatible with a wider range of random number generators. It also updates std::uniform_int_distribution to use decltype.

In Details

The ranges::sample and ranges::shuffle algorithms in <bits/ranges_algo.h> and std::uniform_int_distribution in <bits/uniform_int_dist.h> were assuming the existence of URBG::result_type. The fix replaces this with decltype(__g()) and decltype(__urng()) respectively. This aligns the implementation with the std::uniform_random_bit_generator concept.

For Context

Random number generators are used in many applications, including simulations, games, and cryptography. The C++ standard library provides a set of tools for working with random numbers. This commit fixes a bug in the C++ standard library that made it difficult to use certain types of random number generators with the ranges::sample and ranges::shuffle algorithms. The fix makes these algorithms more flexible and easier to use.

Filed Under: libstdc++random number generatorsranges