import random


def random_state(primes: dict, subspace={}):
    """
    Generates a random state of the transition system defined by *Primes*.
    If *Subspace* is given then the state will be drawn from that subspace.

    **arguments**:
        * *Primes*: prime implicants
        * *Subspace* (str/dict): a subspace

    **returns**:
        * *State* (dict): random state inside *Subspace*

    **example**::

        >>> random_state(primes)
        {'v1':1, 'v2':1, 'v3':1}
        >>> random_state(primes, {"v1":0})
        {'v1':0, 'v2':1, 'v3':0}
        >>> random_state(primes, "0--")
        {'v1':0, 'v2':0, 'v3':1}
    """
    
    if type(subspace) == str:
        assert (len(subspace) == len(primes))
        x = {}
        for name, value in zip(sorted(primes), subspace):
            if value.isdigit():
                x[name] = int(value)
        subspace = x
    else:
        assert (set(subspace).issubset(set(primes)))
    
    items = list(subspace.items()) + [(x, random.choice([0, 1])) for x in primes if x not in subspace]
    
    return dict(items)