# 99 bugs in the code

The adaption of "99 bottles of beer on the wall" for computer science where the bugs increase instead of the bottles decreasing is often re-posted around the internet. Example T-Shirt Here.

I think it'll be interesting to see potential recursion and random number generation across a huge variety of languages and finding the most efficient ways to do it.

There's a fair few other challenges to do with 99 bottles of beer but none seem to have an increasing and decreasing number!

# Challenge

Your program or function should take no input and then print

99 bugs in the code

99 bugs in the code

Take one down and patch it around

X bugs in the code

(blank line)

Where X is the previous integer minus 1 plus a random integer in the range [-15,5].
You can merge the minus 1 into the random integer, hence allowing the range [-16,4].
Ranges can be exclusive, so minus one plus (-16,6) or (-17,5).

The random integers don't have to be evenly distributed they just have to all be possible.

The program always starts with 99 bugs.

You can ignore the grammatical error of "1 bugs".

The program should stop when the number of bugs is 0 or negative and print

0 bugs in the code

There should never be a negative number of bugs. The ending should look like

Y bugs in the code

Y bugs in the code

Take one down and patch it around

0 bugs in the code

(blank line)

0 bugs in the code

A trailing new line is acceptable.

• Your code can be a full program or a function.
• There is no input.
• The output should be to stdout or returned.
• Warnings/errors in logs/STDERR are okay as long as STDOUT has the required text. See here for more info.

This is code-golf so the shortest code in bytes wins.

# Example Output

Paste bin example output rigged for -11 bugs each time

• Related: 1 2 (difference: in this challenge the output can be arbitrarily long). – user202729 May 29 '18 at 14:16
• A more realistic scenario would be if the sign of the random number was flipped! – Stewie Griffin May 29 '18 at 15:21
• I am disappointed, that the requirements don't include that the program must have a bug once a negative number is encountered, like crashing, overflowing to max int or similar ;). – allo May 30 '18 at 9:58
• "The random integers don't have to be evenly distributed they just have to all be possible." reminded me of xkcd.com/221 – Ivo Beckers May 30 '18 at 12:33
• It's a shame 99 has no random number generation. – Jonathan Allan May 30 '18 at 21:07

# C (GCC), 138 bytes

i=99;main(v){for(;i-v--;printf("%d bugs in the code\n%s",i*=i>0,v?v+1?i-=rand()%21-4,"\n":"Take one down and patch it around\n":""))v%=3;}


Note: I managed to get it down to 141 bytes by myself (no external help); later I saved three bytes by applying a technique that ErikF used (i*=i>0 instead of i=i<0?0:i). My original solo version:

i=99;main(v){for(;i-v--;printf("%d bugs in the code\n%s",i=i<0?0:i,v?v+1?i-=rand()%21-4,"\n":"Take one down and patch it around\n":""))v%=3;}

• Nice golf! Also, you should provide a way to test the code - for example, a link to tio.run – user77406 Nov 9 '18 at 20:19

# Python 3, 187 bytes

from random import randint as r
p=print;b=99
def l(x): p(x if x > 0 else 0,"bugs in the code.")
while b > 0: l(b);l(b);p("Take one down and patch it around.");b+=r(-16,4);l(b);p("")
l(0)


So it turns out that dealing with IO/Random in Haskell is a nightmare, so changed the solution to Python. First attempt, not too great :( But at least it's working.

# javascript: 142

i=99
t= bugs in the code
;s='';while(i){s+=i+t+i+t+Take one down and patch it around
;i+=4-Math.random()*20|0;i=i<0?0:i;s+=i+t+
}s+=i+t;


Try it online

# Bash, 182178151 149 bytes

i=99;b=" bugs in the code";a(){ echo $i$b;};while((i>0));do a;a;echo Take one down and patch it around;i=$[i-$RANDOM%21+4];((i<0))&&i=0;a;echo;done;a


Try it online!

Ungolfed

i=99;                             # Var i as current number of bugs
b=" bugs in the code";            # b to shortcut our string

a() {                             # Define function 'a'
echo $i$b;                    # to shortcut displaying or number of bugs
};

while((i>0)); do                  # Repeat as long as var i is greater than 0
a;a                           # Execute 'a' twice
echo Take one down and...     # Display some bugs taken down
i=                            # Assign the following value to $i:$[i-$RANDOM%21+4] # i minus a random value between -4 and 16 #$RANDOM is a bash builtin function to generate a
#     pseudorandom value between 0 and 32767 inclusive.
#     I have applied a modulo 21 to get 21 possible
#     values. We take advantage of the allowed uneven
#     distribution.

((i<0))                       # Perform the expression i < 0
&&                     # and assign 0 to i only if the previous
i=0                  # command completed successfully

a                             # Execute 'a'
echo                          # Display a blank line
done
a                                 # Execute 'a'


Thanks to muru and Sam Dean for their improvements, they saved me 31 bytes.

• can't you remove the " -e" after the first echo? – Sam Dean May 31 '18 at 14:52
• removed the " -e" and a whitespace "; i" Try it online! – Sam Dean May 31 '18 at 14:53
str2:   db 'Take one down and patch it around', 13, 10, '$' ten dw 10 twentyone dw 21  • p.s. with upper 60000+, if not consider it running up so high (in 250) can save some bytes – l4m2 May 30 '18 at 16:47 # Ruby, 128 bytes N=99 def f;puts"#{N} bugs in the code"end (f;f puts"Take one down and patch it around" N-=[N,15-rand(21)].min f puts)while 0<N f  Try it online! # Japt -R, 837975 73 bytes Includes a trailing newline #c  Þï e ¬¸  @§T}a@Np[UVUVìÇ¦ÓnÓlp®äNh qÊ¸TwU±Gn#ö¹V]¬ÃpViT  Test it ## Explanation #c :99 \n :Assign to variable U ... :The compressed string " bugs in the code\n" \n :Assign to variable V [UVUV V] :Build an array with U as the 1st & 3rd elements, : V as the 2nd, 4th & 7th : and the following as the 5th & 6th: ... : The compressed string "Takeloneldownlandlpatchlitlaround\n" qÊ : Split on "l" ¸ : Join with spaces w ¹ : Maximum of T : 0 and # : 21 ö : Random number in the range [0,21) n : Subtract G : 16 U± : Add to U ¬ :Join to a string Np :Push to N, initially an empty array @ }a@ Ã :Repeat until §T : U is less than or equal to 0 p :Push ViT : V prepended with 0 :Implicitly join with newlines and output  # BLZ 2.6, 155 bytes b=99 while b>0 n={text.newline} s=" bugs in the code"+n p=(x->print(99+s+99+s+"Take one down and patch it around"+n+b+s)) p() b=b+random(-16,5) end b=0 p()  Reminds me that I need to make "\n" a newline so I can save some of those characters from {text.newline} • anyway we can test that? also can you remove the space before "5" in the random function? – Sam Dean Jun 6 '18 at 10:15 • @SamDean Good call on the space, down to 155 bytes. You can test it by installing blz if you want. There's a guide here github.com/blazingkin/blz-ospl/wiki/Installation You'd also have to checkout the v2.6 branch – blazingkin Jun 6 '18 at 18:44 # C# (.NET Core), 175 bytes var s="";var r=new Random();for(int p=99;p>0;){var d=" bugs in the code\n";s+=p+d+p+d+"Take one down and patch it around\n";p+=r.Next(-17,5);s+=(p>0?p:0)+d+"\n";}return s;  Try it online! C# uses a class for randomizing. Without the "Random" object, the response would be around 148 bytes. Using a for loop instead of a while loop removes one byte. using "new Random.Next()" will not work, since creating multiple Random objects within a loop wont make the Randomizer actually random. • Welcome to PPCG. – Muhammad Salman Jun 5 '18 at 22:46 • You need to add that final "0 bugs in the code" but nice answer! – Sam Dean Jun 6 '18 at 10:13 # Clojure, 192 188 bytes #(loop[b 99](def s"bugs in the code")(printf"%s %s\n%s %s\nTake one down and patch it around\n"b s b s)(def n(max(+ b(rand-int 21)-16)0))(println n s"\n")(if(= n 0)(println n s)(recur n)))  Try it online! ## Explanation #(loop [b 99] ; Initialize a loop with b=99 (def s "bugs in the code") ; Define a variable s that equals "bugs in the code" (printf "%s %s\n%s %s\nTake one down and patch it around\n" b s b s) ; Prints ; "99 bugs in the code ; 99 bugs in the code ; Take one down and patch it around" with the correct formatting. (def n (max (+ b (rand-int 21) -16) 0)) ; Let n = max(b+<some random number between -16 and 4 inclusive>, 0) This prevents negative numbers by replacing them with 0 (println n s "\n") ; Prints "<n> bugs in the code" followed by an extra newline (if (= n 0) ; If n equals 0 (println n s) ; Prints "<n> bugs in the code" (recur n))) ; Otherwise run the loop again with b=n  # Dart, 173 169 bytes import'dart:math';f({n=99,m=' bugs in the code\n'}){while(n>0){print('$n$m$n$m\Take one down and patch it around');n=n+Random().nextInt(20)-16;n=n<0?0:n;print('$n\$m');}}


Try it online!

Almost competing with other Java-like languages for once