Skip to content

Commit 4097056

Browse files
committed
always use 8 bits for draw_boolean, cap falsey to 1
1 parent be289ab commit 4097056

File tree

1 file changed

+6
-6
lines changed
  • hypothesis-python/src/hypothesis/internal/conjecture

1 file changed

+6
-6
lines changed

hypothesis-python/src/hypothesis/internal/conjecture/providers.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ def draw_boolean(
6565
if p >= 1:
6666
return True
6767

68-
bits = math.ceil(-math.log2(min(p, 1 - p)))
69-
# cap boolean draws at 8 bits so they are a constant size of one byte.
70-
# If a probability requires more than 8 bits to precisely represent,
68+
# always use one byte for booleans to maintain constant draw size.
69+
# If a probability requires more than 8 bits to represent precisely,
7170
# the result will be slightly biased, but not badly.
72-
bits = min(bits, 8)
73-
71+
bits = 8
7472
size = 2**bits
75-
falsey = math.floor(size * (1 - p))
73+
# always leave at least one value that can be true, even for very small
74+
# p.
75+
falsey = max(1, math.floor(size * (1 - p)))
7676
n = self._draw_bits(bits)
7777
return n >= falsey
7878

0 commit comments

Comments
 (0)