import random


def sample(items, k, replace=False):
    """
    Randomly sample k items with equal probability.
    """
    if not replace and k > len(items):
        raise ValueError("Sample must be no larger than population.")
    remaining = items
    sample = []
    for i in range(k):
        n = random.randint(0,len(remaining)-1)
        sample.append(remaining[n])
        if not replace:
            remaining = remaining[0:n] + remaining[n+1:]
    return sample