import itertools
import random


def generate_random_latency_map(relays, minval = 1, maxval = 20):
  """ Generate a random set of pairwise latencies for the
      relays

  :relays: the relays which should be included in the latency map
  :minval: the minimum latency
  :maxval: the maximum latency
  :returns: a dictionary which represents a latency map.

  """
  lmap = dict()
  for r1,r2 in itertools.combinations(map(lambda x: x[0],relays),2):
    try:
      lmap[r1][r2] = random.randint(minval,maxval)
    except KeyError:
      lmap[r1] = dict()
      lmap[r1][r2] = random.randint(minval,maxval)

  return lmap