code: def biased_coin(data, p):
      """Return True with probability p (assuming a uniform generator),
      shrinking towards False."""
      data.start_example(BIASED_COIN_LABEL)
      while True:
          # The logic here is a bit complicated and special cased to make it
          # play better with the shrinker.
  
          # We imagine partitioning the real interval [0, 1] into 256 equal parts
          # and looking at each part and whether its interior is wholly <= p
          # or wholly >= p. At most one part can be neither.
  
          # We then pick a random part. If it's wholly on one side or the other
          # of p then we use that as the answer. If p is contained in the
          # interval then we start again with a new probability that is given
          # by the fraction of that interval that was <= our previous p.
  
          # We then take advantage of the fact that we have control of the
          # labelling to make this shrink better, using the following tricks:
  
          # If p is <= 0 or >= 1 the result of this coin is certain. We make sure
          # to write a byte to the data stream anyway so that these don't cause
          # difficulties when shrinking.
          if p <= 0:
              data.draw_bits(1, forced=0)
              result = False
          elif p >= 1:
              data.draw_bits(1, forced=1)
              result = True
          else:
              falsey = floor(256 * (1 - p))
              truthy = floor(256 * p)
              remainder = 256 * p - truthy
  
              if falsey + truthy == 256:
                  if isinstance(p, Fraction):
                      m = p.numerator
                      n = p.denominator
                  else:
                      m, n = p.as_integer_ratio()
                  assert n & (n - 1) == 0, n  # n is a power of 2
                  assert n > m > 0
                  truthy = m
                  falsey = n - m
                  bits = bit_length(n) - 1
                  partial = False
              else:
                  bits = 8
                  partial = True
  
              i = data.draw_bits(bits)
  
              # We always label the region that causes us to repeat the loop as
              # 255 so that shrinking this byte never causes us to need to draw
              # more data.
              if partial and i == 255:
                  p = remainder
                  continue
              if falsey == 0:
                  # Every other partition is truthy, so the result is true
                  result = True
              elif truthy == 0:
                  # Every other partition is falsey, so the result is false
                  result = False
              elif i <= 1:
                  # We special case so that zero is always false and 1 is always
                  # true which makes shrinking easier because we can always
                  # replace a truthy block with 1. This has the slightly weird
                  # property that shrinking from 2 to 1 can cause the result to
                  # grow, but the shrinker always tries 0 and 1 first anyway, so
                  # this will usually be fine.
                  result = bool(i)
              else:
                  # Originally everything in the region 0 <= i < falsey was false
                  # and everything above was true. We swapped one truthy element
                  # into this region, so the region becomes 0 <= i <= falsey
                  # except for i = 1. We know i > 1 here, so the test for truth
                  # becomes i > falsey.
                  result = i > falsey
          break
      data.stop_example()
      return result
  
imports: from __future__ import absolute_import, division, print_function
  import enum
  import hashlib
  import heapq
  import sys
  from collections import OrderedDict
  from fractions import Fraction
  from hypothesis.errors import InvalidArgument
  from hypothesis.internal.compat import abc, bit_length, floor, hrange, int_from_bytes, qualname, str_to_bytes
  from hypothesis.internal.floats import int_to_float
label_desc: Use image node_id 9 for calling a global function with example usage: biased_coin(data, p) and returns: result