import random


def backoff_time(attempt, retry_backoff=2., max_delay=30.):
  """Compute randomized exponential backoff time.

  Args:
    attempt (int): attempt number, starting at zero.

  Keyword Args:
    retry_backoff(float): backoff time on the first attempt.
    max_delay(float): maximum returned value.
  """
  delay = retry_backoff * (2 ** attempt)
  # Add +-25% of variation.
  delay += delay * ((random.random() - 0.5) / 2.)
  return min(delay, max_delay)