How do I write a doctest in python 3.5 for a function using random to replace characters in a string?
<pre><code>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
</code></pre>

<p>You need to mock the random functions to give something that is predetermined.</p>

<pre><code>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
    &gt;&gt;&gt; import random
    &gt;&gt;&gt; myrand = lambda x, y: 1
    &gt;&gt;&gt; mychoice = lambda x: "T"
    &gt;&gt;&gt; random.randint = myrand
    &gt;&gt;&gt; random.choice = mychoice
    &gt;&gt;&gt; intoxication("Hello World")
    'Hello World'

    If there is 100% chance of a random character chosen, result will be the same as 'TTTTTTTTTTT'
    &gt;&gt;&gt; import random
    &gt;&gt;&gt; myrand = lambda x, y: 0
    &gt;&gt;&gt; mychoice = lambda x: "T"
    &gt;&gt;&gt; random.randint = myrand
    &gt;&gt;&gt; random.choice = mychoice
    &gt;&gt;&gt; intoxication("Hello World")
    'TTTTTTTTTTT'

    If every second character is replaced
    &gt;&gt;&gt; import random
    &gt;&gt;&gt; thisone = 0
    &gt;&gt;&gt; def myrand(x, y): global thisone; thisone+=1; return thisone % 2
    &gt;&gt;&gt; mychoice = lambda x: "T"
    &gt;&gt;&gt; random.randint = myrand
    &gt;&gt;&gt; random.choice = mychoice
    &gt;&gt;&gt; intoxication("Hello World")
    'HTlToTWTrTd'

    If every third character is replaced
    &gt;&gt;&gt; import random
    &gt;&gt;&gt; thisone = 0
    &gt;&gt;&gt; def myrand(x, y): global thisone; thisone+=1; return thisone % 3
    &gt;&gt;&gt; mychoice = lambda x: "T"
    &gt;&gt;&gt; random.randint = myrand
    &gt;&gt;&gt; random.choice = mychoice
    &gt;&gt;&gt; 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()
</code></pre>

python python-3.x