Skip to content

Fix Box.sample never returning values near signed integer dtype limits - #1666

Merged
pseudo-rnd-thoughts merged 1 commit into
Farama-Foundation:mainfrom
shoutoutuoadi325:fix/box-sample-signed-dtype-limits
Aug 16, 2026
Merged

pseudo-rnd-thoughts merged 1 commit into
Farama-Foundation:mainfrom
shoutoutuoadi325:fix/box-sample-signed-dtype-limits

Conversation

@shoutoutuoadi325

Copy link
Copy Markdown
Contributor

Description

Box.sample() clips integer samples to the dtype's range before casting, but for every signed integer dtype it applied a [dtype_min + 2, dtype_max - 2] margin (introduced in #774). That margin only guards against float64's inexact representation of the int64 limits — the same reason #774 added the int64-specific re-clip after the cast — while the int8/int16/int32 limits are exactly representable in float64. The margin therefore only truncated the sampled support:

import numpy as np
from gymnasium.spaces import Box

space = Box(low=125, high=127, dtype=np.int8)
print([space.contains(np.array([v])) for v in (125, 126, 127)])
# [True, True, True]
space.seed(0)
print({int(v) for _ in range(3000) for v in space.sample()})
# {125} — 126 and 127 can never be sampled

A full-range Box(-128, 127, dtype=np.int8) can never sample -128, -127, 126 or 127 (only 252 of 256 values are reachable), while unsigned dtypes were exempt from the margin, so Box(253, 255, dtype=np.uint8) samples all three values — the asymmetry suggests an oversight rather than a distribution choice.

This restricts the margin to np.int64, mirroring the existing if self.dtype == np.int64: special case directly below it, so int64 behaviour is unchanged. Adds a parametrised regression test asserting that a bounded integer Box samples every value in [low, high] for int8/int16/int32, with uint8 as a control.

This restores the documented contract (docstring: "[a, b]: uniform distribution") and the invariant test_valid_low_high already asserts via space.contains(sample); its parameter list just never used bounds near a signed dtype's limits.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have run the pre-commit checks with pre-commit run --all-files (see CONTRIBUTING.md instructions to set it up)
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Note: in my environment the ty hook reports the same 353 pre-existing diagnostics on unmodified main (missing optional dependencies); this diff adds no new ty diagnostics. ruff, ruff-format and pydocstyle pass.

Box.sample() clips integer samples to the dtype range before casting, but
for every signed integer dtype it applied a [dtype_min + 2, dtype_max - 2]
margin (introduced in #774). That margin only guards against float64's
inexact representation of the int64 limits, which is why #774 also added
the int64-specific re-clip after the cast; int8/int16/int32 limits are
exactly representable in float64, so the margin only truncated the
sampled support. As a result, Box(125, 127, dtype=np.int8) could only
ever sample 125 even though contains() accepts 125, 126 and 127, and a
full-range int8 Box could never sample -128, -127, 126 or 127. Unsigned
dtypes were exempt, so Box(253, 255, dtype=np.uint8) sampled all three
values.

Restrict the margin to np.int64, mirroring the existing int64 special
case directly below it, and add a regression test asserting that a
bounded integer Box samples every value in [low, high] for int8,
int16, int32 (and uint8 as a control).
Copilot AI lite review requested due to automatic review settings August 16, 2026 05:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pseudo-rnd-thoughts pseudo-rnd-thoughts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, looks good

@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit b8e70f2 into Farama-Foundation:main Aug 16, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants