from typing import Optional
import random


def random_subset(a, *, prob=0.5, min_size: Optional[int] = None, max_size: Optional[int] = None):
    """Choose a random subset of a.

    Args:
        a: the input sequence
        prob: Probability to pick an element
            (when called without size constraints).
        min_size: Minimal size of the result.
        max_size: Maximal size of the result.
    Returns:
        A random subset of a
    """
    def subset(a, prob):
        res = []
        remaining = []
        for el in a:
            if random.random() < prob:
                res.append(el)
            else:
                remaining.append(el)
        return res

    if min_size is None:
        min_size = 0
    if max_size is None:
        max_size = len(a)
    min_size = max(min_size, 0)
    max_size = min(max_size, len(a))

    if min_size > max_size:
        raise ValueError(f'Invalid size range for random_subset: [{min_size}, {max_size}]')

    tmp = random.sample(a, max_size)
    return tmp[:min_size] + subset(tmp[min_size:], prob)