How do I write a doctest in python 3.5 for a function using random to replace characters in a string?
Question: 
    def intoxication(text):
        """This function causes each character to have a 1/5 chance of being replaced by a random letter from the string of letters
        INSERT DOCTEST HERE
        """
        import random
        string_letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        text = "".join(i if random.randint(0,4) else random.choice(string_letters) for i in text)   
    
        return text
    


Answer: You need to mock the random functions to give something that is predetermined.

    
    
    def intoxication(text):
        """This function causes each character to have a 1/5 chance of being replaced by a random letter from the string of letters
    
        If there is 0% chance of a random character chosen, result will be the same as input
        >>> import random
        >>> myrand = lambda x, y: 1
        >>> mychoice = lambda x: "T"
        >>> random.randint = myrand
        >>> random.choice = mychoice
        >>> intoxication("Hello World")
        'Hello World'
    
        If there is 100% chance of a random character chosen, result will be the same as 'TTTTTTTTTTT'
        >>> import random
        >>> myrand = lambda x, y: 0
        >>> mychoice = lambda x: "T"
        >>> random.randint = myrand
        >>> random.choice = mychoice
        >>> intoxication("Hello World")
        'TTTTTTTTTTT'
    
        If every second character is replaced
        >>> import random
        >>> thisone = 0
        >>> def myrand(x, y): global thisone; thisone+=1; return thisone % 2
        >>> mychoice = lambda x: "T"
        >>> random.randint = myrand
        >>> random.choice = mychoice
        >>> intoxication("Hello World")
        'HTlToTWTrTd'
    
        If every third character is replaced
        >>> import random
        >>> thisone = 0
        >>> def myrand(x, y): global thisone; thisone+=1; return thisone % 3
        >>> mychoice = lambda x: "T"
        >>> random.randint = myrand
        >>> random.choice = mychoice
        >>> intoxication("Hello World")
        'HeTloTWoTld'
    
        """
        import random
        string_letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        text = "".join(i if random.randint(0,4) else random.choice(string_letters) for i in text)   
    
        return text
    
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
    

