Randomly Misspell a Word Inspired by this CR question (please don't kill me for browsing CR)

Spec

The probabilities of misspelling a word are:


1/3 of the time don't change the output
1/3 of the time remove a random character
1/3 of the time duplicate a random character


The chance for removing/duplicating a given character in the input should be the same for all chars.

If two consecutive characters are the same (case-sensitive), the probability of one of them being modified should be the same as if they are one character. I.e. the outputs for AA (which are AA or A or AAA) should all have the same probability.



The input will only contain of letters for simplicity.

Examples

First line is input, following lines are all possible misspellings. Each line should have the same probability of being output, the input is excluded in the examples but it should still have a 1/3 probability of output.

foo

fo
oo
ffoo
fooo




PPCG

PPC
PPG
PCG
PPPCG
PPCCG
PPCGG
Pip, 38 27 bytes

a@:`(.)\1*`YRR#aa@y@0X:RR3a


This was fun--got to use Pip's regex and mutable string capabilities, which I hadn't pulled out in a while. Takes input via command-line argument.

Explanation:

a@:`(.)\1*`                  Split a into runs of identical chars using regex match
           YRR#a             Yank randrange(len(a)) into y (randomly choosing one such run)
                a@y@0        Take the first character of that run
                     X:RR3   Modify in place, string-multiplying by randrange(3):
                               If RR3 is 0, character is deleted
                               If RR3 is 1, no change
                               If RR3 is 2, character is duplicated
                          a  Output the modified a


Try it online!