from typing import Optional
import random


def random_permutation(
        iterable,
        *,
        items: Optional[int] = None,
        rng: Optional[random.Random] = None,
):
    """

    >>> from random import Random
    >>> random = Random(1)
    >>> suits = 'CDHS'
    >>> ranks = list(range(2, 11)) + ['J', 'Q', 'K', 'A']
    >>> import itertools
    >>> def make_deck(): return itertools.product(ranks, suits)
    >>> len(list(make_deck()))
    52
    >>> hand = random_permutation(make_deck(), items=5, rng=random)
    >>> hand
    ((4, 'C'), ('J', 'C'), ('A', 'S'), ('A', 'C'), (3, 'C'))
    >>> shuffled_deck = random_permutation(make_deck(), rng=random)
    >>> len(list(shuffled_deck))
    52
    >>> shuffled_deck[:5]
    ((6, 'C'), (3, 'S'), (9, 'S'), ('A', 'C'), (9, 'C'))

    """
    sample = random.sample if rng is None else rng.sample
    space = tuple(iterable)
    items = len(space) if items is None else items
    return tuple(sample(space, items))