Skip to content

st.integers() prefers values, closer to min_value for bounded integers on big enough interval #4624

@skirpichev

Description

@skirpichev

Simple example, here is unbounded case:

>>> import hypothesis.strategies as st
>>> n    = 5000
... ints = st.integers()
... cnt  = 0
... for _ in range(n):
...     if ints.example() < 0:
...         cnt += 1
... print(f"{cnt/n:.0%}")
... 
46%

Here is a small interval (bound are symmetrical):

>>> bits = 16
... mval = (1<<bits) - 1
... ints = st.integers(min_value=-mval, max_value=mval)
... cnt  = 0
... for _ in range(n):
...     if ints.example() < 0:
...         cnt += 1
... print(f"{cnt/n:.0%}")
... 
46%

But what happens with bigger bounds?

Distribution looks asymmetrical:

>>> bits = 32
... mval = (1<<bits) - 1
... ints = st.integers(min_value=-mval, max_value=mval)
... cnt  = 0
... for _ in range(n):
...     if ints.example() < 0:
...         cnt += 1
... print(f"{cnt/n:.0%}")
... 
79%
>>> bits = 64
... mval = (1<<bits) - 1
... ints = st.integers(min_value=-mval, max_value=mval)
... cnt  = 0
... for _ in range(n):
...     if ints.example() < 0:
...         cnt += 1
... print(f"{cnt/n:.0%}")
... 
81%

I'm giving examples with, huh, examples(), but same happens for regular tests with pytest&hypothesis.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsomething is clearly wrong here

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions