From 075be2a0f11cf93076d3d207c050f79681ea1cf0 Mon Sep 17 00:00:00 2001 From: AHMETHAKANBEZIR1 Date: Fri, 2 Oct 2026 06:41:32 +0300 Subject: [PATCH] Use explicit int64 sampling for integer parameter bounds Fixes #623 Co-authored-by: Codex --- bayes_opt/parameter.py | 4 +++- tests/test_parameter.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/bayes_opt/parameter.py b/bayes_opt/parameter.py index a8955e5b..70b5aa91 100644 --- a/bayes_opt/parameter.py +++ b/bayes_opt/parameter.py @@ -278,7 +278,9 @@ def random_sample( The samples. """ random_state = ensure_rng(random_state) - return random_state.randint(self.bounds[0], self.bounds[1] + 1, n_samples).astype(float) + return random_state.randint(self.bounds[0], self.bounds[1] + 1, n_samples, dtype=np.int64).astype( + float + ) def to_float(self, value: int | float) -> float: """Convert a parameter value to a float. diff --git a/tests/test_parameter.py b/tests/test_parameter.py index 752656de..f067d771 100644 --- a/tests/test_parameter.py +++ b/tests/test_parameter.py @@ -87,6 +87,20 @@ def target_func(**kwargs): assert p1.kernel_transform(np.array([1.3, 3.6, 7.2])) == pytest.approx(np.array([1, 4, 7])) +@pytest.mark.parametrize("bounds", [(3_000_000_000, 3_000_000_010), (-3_000_000_010, -3_000_000_000), (0, 5)]) +def test_int_random_sample_large_bounds(bounds): + parameter = IntParameter("x", bounds) + samples = parameter.random_sample(100, random_state=np.random.RandomState(42)) + repeated = parameter.random_sample(100, random_state=np.random.RandomState(42)) + assert samples.dtype == np.dtype(float) + np.testing.assert_array_equal(samples, repeated) + assert np.all(samples >= bounds[0]) + assert np.all(samples <= bounds[1]) + assert np.all(samples == np.floor(samples)) + assert bounds[0] in samples + assert bounds[1] in samples + + def test_cat_parameters(): fruit_ratings = {"apple": 1.0, "banana": 2.0, "mango": 5.0, "honeydew melon": -10.0, "strawberry": np.pi}