--

[first published April 18th, 2023; revised July 3th, 2023]

Hi Fatih, haha, that good monkey is an old favourite 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 "afriMakdnhfOjahudgNKfksikkosdfckEjjdjadY"). 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, a first guess is that there are about n possibilities to get that right; if that way of looking is correct, we have a binomial Bin(n,p), and the expected number of ‘MONKEY’-s we’ll get with a binomial is n*p. That’s n*(1/26)⁶: we need to let the little beast hit the keys some 300 million times to make this expected number of ‘MONKEY’-s equal to one (26⁶ = 308 915 776).

Hmm … But is it correct? Is it really a binomial? No. It is not. The problem is that the ‘hits’, in case the monkey just types on and on and on, are, we might say: not ‘independent’, due to possible overlap of parts, like if the monkey e.g. writes ‘EMAMON’, then this ‘miss-string’ actually contains already half of what might turn into a ‘hit’. E.g., in case the monkey continues to produce ‘KEYPAD’, she needed only three more characters to make the ‘MONKEY’ complete.

So, it’s a bit more subtle than the above first ‘binomial’ try suggests. However, the little script that follows does act in the ‘binomial way’: we let the monkey type a string of the desired length, check if it is OK. If it is not, we erase what we had, and start again. That is binomial. It will be close, but NOT quite the same as estimating how long on the average we need to wait for the first ‘MONKEY’ to occur if we let our monkey type on and on and on.

[ Maybe some time later I’ll come back to how to do fully correct calculation. ]

Here’s the ‘binomial try’:

`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)