Hi Fatih, haha, that good monkey is an old favorite of mine :-) ... But, correct me if I’m wrong, isn’t your R-thing just counting how long it takes to randomly get the letters ‘m’, ‘o’, ‘n’, ‘k’, ‘e’, ‘y’ , in that order, but not necessarily the one right after the other? That is not the same thing I’d be interested in. Generating the STRING ‘monkey’ I’d rather take to mean that the letters are immediate successors of one another: we need see MONKEY written on the monkey’s page/screen, and not, something like "afriMakdnohfOjahudgNKeefksikkosdfckEjjdjadY"). While the probability to generate the STRING ‘monkey’ is (1/26)^6, your R-thing counts how long it takes to get these 6 letters m,o,n,k,e,y. Randomly. But allowing arbitrary long ‘gaps’ that are filled up by other characters.

Now, the probability that *that* happens for the first time after *k* monkey-hits (with *k* some number bigger than or equal to 6) is: ((k-1) choose (k-6))*(1/26)^6*(25/26)^(k-6).

The expected value of this random variable is the sum of *k* times this probability, with *k* running from 6 to infinity.

If you calculate that [ sum (k*((k-1) choose (k-6))*((1/26)^6)*(25/26)^(k-6)) for k = 6 to infinity ] (for example by pasting the part between straight brackets in Wolfram Alpha, the answer turns out to be 156. Pretty close to your count. Hence, not so surprising after all.

To do the same for "monkey wrote this passage", one calculates the sum (k*((k-1) choose (k-25))*((1/27)^25)*(26/27)^(k-25)) ] for k = 25 to infinity. Now the expected value (the average number of times monkey will need to hit a letter key to get all the 25 characters) is 675. Again, not so very surprising hence, that your count was 744.

(We use 1/27 and 26/27, as there now also is a ‘space’ included in the set of characters.)

If you type a R-thingy that DOES look whether the characters in a given string like 'MONKEY' occur *consecutively* in a shower of random monkey-hits, the numbers you get turn out pretty close to the 'powers of 1/26' that you mentioned in your write up: the result ‘MONKEY’ will randomly occur with probability *p = *(1/26)⁶; so if we let monkey hit the keys *n+5* times, there’s *n *possibilities to get that right; that’s a binomial *Bin(n,p)*, and the expected number of ‘MONKEY’-s we’ll get is* n*p. *That’s n*(1/26)⁶, so likely the little beast will need to hit the keys at least some *300 million* times before coming up with a single ‘MONKEY’ (26⁶ = 308 915 776).

`monkey <-function(x){`

chars <- c(letters, " ")

goal <- x

count <- 0

repeat {

monkey_wrote <- vector()

for (j in 1:nchar(x)){

monkey_wrote<- c(monkey_wrote, sample(chars,1))

}

monkey_wrote <- paste(monkey_wrote, collapse='')

if(monkey_wrote==goal){cat("monkey tried", count, "times to make", goal)

break}

else {count=count+1}

}

}

Give *monkey* a goal *x*, a string made of letters of the british alphabet and spaces, and she will tell you how many random tries it took her to do so. Some example results:

`> monkey("me")`

monkey tried 146 times to make me

> monkey("ur")

monkey tried 684 times to make ur

> monkey("tar")

monkey tried 23201 times to make tar

> monkey("war")

monkey tried 31236 times to make war

> monkey("art")

monkey tried 58513 times to make art

> monkey("love")

monkey tried 1069417 times to make love

Would not recommend let R run & get you "monkey wrote this passage" as a sequence of consecutive random characters; let alone William Shakespeare’s collected works, which will add up to a string consisting in some 5 million characters.

// 🐒 _^_ °_ ^_\\

OK ... Guess you know Jorge Luis Borges' "Library of Babel", the universal library, containing ALL possible BOOKS that can be written. If not, read that fabulous story, and maybe fun to have a look at Jonathan Basile's online version of the universal library, at https://libraryofbabel.info/

thx for the inspiration !

greetings, HS

[ *There’s more about random-writers, Jorge Luis Borges, Babel and Jonathan Basel in one of the ‘Hoofd Stuk’-s that I write for each printed edition of **Gonzo Circus (Magazine) #146 (July/August 2018)** (in Dutch)*. ]

(also related : http://www.harsmedia.com/SoundBlog/Archief/00860.php)