import random


def partition(elements, portions):
    """ partitions samples randomly.

    Args:
        elements: A list.
        portions: A list of positive real numbers, each between 0 and 1,
            exclusively. The summation of elements in portions must be 1.

    Returns:
        A random partition of samples, where partitions[i] includes
            portions[i] * 100 percent of samples.

    """
    assert sum(portions) == 1
    index_value_map = {idx: value for idx, value in enumerate(elements)}
    random_indices = list(index_value_map.keys())
    random.shuffle(random_indices)
    partitions = []
    total = 0
    start = 0
    for i in range(len(portions) - 1):
        num_elements = int(portions[i] * len(elements))
        total += num_elements
        partition_indices = random_indices[start: total]
        partitions.append([index_value_map[idx] for idx in partition_indices])
    partition_indices = random_indices[total:]
    partitions.append([index_value_map[idx] for idx in partition_indices])
    return partitions