import random


def _determine_value(neighbors, values):
    """
    Returns the value to set an element to based on its neighbors.

    :param neighbors: The neighbors of the element in question.
    :param values: The values of the terrain. This is passed in so that
            a new dictionary doesn't have to be created and added to.

    """
    # Create a dictionary to keep track of the amounts of each value
    counts = {value: 0 for value in values}

    # For each neighbor...
    for neighbor in neighbors:
        # Count it
        counts[neighbor] += 1

    # Create majority value (by default the first value type in counts)
    majority = list(counts.keys())[0]

    # Find the biggest value...
    for value in counts:
        # If this value's count is bigger...
        if counts[value] > counts[majority]:
            # Set majority to it
            majority = value
        # If it ties with the current majority...
        elif counts[value] == counts[majority]:
            # Randomly choose the majority between them
            majority = value if random.random() > 0.5 else majority

    # Return the majority value
    return majority