import uuid


def get_visitor_id(guid, cookie):
    """Generate a visitor id for this hit.
    If there is a visitor id in the cookie, use that, otherwise
    use the guid if we have one, otherwise use a random number.
    """
    if cookie:
        return cookie
    if guid:
        # create the visitor id using the guid.
        cid = guid
    else:
        # otherwise this is a new user, create a new random id.
        cid = str(uuid.uuid4())
    return cid