import random


def get_random_value(world, feature, rng=None):
    """
    Gets a random value for a given feature according to its domain.
    :param World world: the PsychSim world in which the feature is defined.
    :param str feature: the named feature.
    :param random.Random rng: the random state used to sample values.
    :return: a random value according to the feature's domain.
    """
    assert feature in world.variables, 'World does not contain feature \'{}\''.format(feature)

    var = world.variables[feature]
    domain = var['domain']
    rng = random.Random() if rng is None else rng

    if domain is float:
        return rng.uniform(var['lo'], var['hi'])
    if domain is int:
        return rng.randint(var['lo'], var['hi'])
    if domain is list or domain is set:
        return rng.choice(var['elements'])
    if domain is bool:
        return bool(rng.randint(0, 1))
    return None