import random


def random_hash(hash_length, sequence_length, seed=None):
  """Create a hash function for randomized partitioning.

  Args:
    hash_length: integer number of bases from each sequence to use in the hash
      key.
    sequence_length: integer length of all sequences in this partition.
    seed: optional hashable random seed to guarantee reproducible results.

  Returns:
    Hash function suitable for partitioning sequences with an
    ApproximateStrategy.
  """
  rand = random.Random(seed)
  indices = sorted(rand.sample(range(sequence_length), hash_length))

  def hash_func(sequence):
    return ''.join(sequence[idx] for idx in indices)

  return hash_func