def shuffle(collection, random=None):
    """Return a randomly shuffled copy of a collection.

    Note that this function's behavior depends on the type of its input.

    If collection represents an infinite iterable, this will loop indefinitely.
    """
    elements = list(collection)
    random.shuffle(elements, random=random)
    if isinstance(collection, str):
        return ''.join(elements)
    return collection.__class__(elements)
