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

Fix: UniformIntGenerator range error #7143

Merged
merged 4 commits into from
Jan 29, 2025
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
15 changes: 8 additions & 7 deletions cpp/open3d/utility/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ std::mutex* GetMutex();
/// This function is automatically protected by the global random mutex.
uint32_t RandUint32();

/// Generate uniformly distributed random integers in [low, high).
/// Generate uniformly distributed random integers in [low, high].
/// This class is globally seeded by utility::random::Seed().
/// This class is a wrapper around std::uniform_int_distribution.
///
Expand All @@ -54,7 +54,7 @@ uint32_t RandUint32();
/// // Globally seed Open3D. This will affect all random functions.
/// utility::random::Seed(0);
///
/// // Generate a random int in [0, 100).
/// // Generate a random int in [0, 100].
/// utility::random::UniformIntGenerator<int> gen(0, 100);
/// for (size_t i = 0; i < 10; i++) {
/// std::cout << gen() << std::endl;
Expand All @@ -64,17 +64,18 @@ template <typename T>
class UniformIntGenerator {
public:
/// Generate uniformly distributed random integer from
/// [low, low + 1, ... high - 1].
/// [low, low + 1, ... high].
///
/// \param low The lower bound (inclusive).
/// \param high The upper bound (exclusive). \p high must be > \p low.
/// \param high The upper bound (inclusive). \p high must be >= \p low.
UniformIntGenerator(const T low, const T high) : distribution_(low, high) {
if (low < 0) {
utility::LogError("low must be > 0, but got {}.", low);
}
if (low >= high) {
utility::LogError("low must be < high, but got low={} and high={}.",
low, high);
if (low > high) {
utility::LogError(
"low must be <= high, but got low={} and high={}.", low,
high);
}
}

Expand Down