import random


def check_random_state(seed):
    """
    Turn seed into a random.Random instance
    If seed is None, return the Random singleton used by random.
    If seed is an int, return a new Random instance seeded with seed.
    If seed is already a Random instance, return it.
    Otherwise raise ValueError.
    """
    # Code slightly adjusted from scikit-learn utils/validation.py
    if seed is None or isinstance(seed, int):
        rng = random.Random(seed)
    elif isinstance(seed, random.Random):
        rng = seed
    else:
        raise ValueError(
            "### error: '{}' cannot be used to seed random.Random instance.".format(
                seed
            )
        )
    return rng