g | x | w | all
Bytes Lang Time Link
146Swift 6250502T143609ZmacOSist
071Japt180530T225403ZShaggy
247COBOL GnuCOBOL180531T100111ZMC Emper
115Julia 0.7210504T134145ZMarcMush
165C180529T152326ZSteadybo
131PowerShell180529T150008ZAdmBorkB
145SAS181126T041444Zwhymath
169Dart181126T101931ZElcan
188Clojure181110T011345ZTheGreat
171Python 3181109T154153Zglietz
149Bash180531T144453ZMC Emper
240LaTeX180602T145432ZSimon Kl
127JavaScript Node.js180529T151351ZMuhammad
163VBA 163 Bytes180602T025819ZBen
142javascript180601T173436ZStr34k
175C# .NET Core180605T223955ZScott Ar
155BLZ 2.6180605T230707Zblazingk
187Python 3180604T124453ZKamil Ja
138C GCC180601T181437Zdavid.we
137C gcc180529T195345ZErikF
128Ruby180601T163635ZReinstat
133Zsh180601T072634Zmuru
149Ruby180531T170301ZDaveMong
127Python 2180530T222301ZJonathan
138Python 3180529T150312ZJo King
nan180531T131611ZChronoci
181C#180531T082719ZBen Nobl
135R180529T150345ZGiuseppe
162JavaScript180529T143149ZThe rand
160Java 8180529T145357ZKevin Cr
132Perl180531T032151ZTrenton
061Jelly180530T002837ZJonathan
234Clean180529T232318ZΟurous
137.COM opcode180530T164339Zl4m2
092Pyth180530T131139ZSok
295ABAP180530T145458ZMaz
107Japt180529T154257Zuser3604
163Red180530T090403ZGalen Iv
081Charcoal180529T204726ZNeil
134QB64180530T024628ZErikF
138JavaScript180530T011954Zdarrylye
126PHP180530T002050ZPleaseSt
188TSQL180529T195042ZBradC
210SAS180529T170039ZJoe
144Python 3180529T162830Zuser2699
148Octave180529T150934ZStewie G
151Python 2180529T151437ZDead Pos
05905AB1E180529T152947ZEmigna

Swift 6, 148 146 bytes

var b=99
var s:(){print(b,"bugs in the code")}
while b>0{s
s
print("Take one down and patch it around")
b-=min(.random(in:-4...16),b)
s
print()}
s

Try it on SwiftFiddle! (For whatever reason, SwiftFiddle doesn't seem to print empty lines correctly — adding " " to the final call to print() fixes this. This isn't an issue when running it locally.)

Japt, 83 79 75 73 71 bytes

ª99
>0?[UV=` Þï  e ¬¸
`UV`ìÇsç.Àsp®äVh
`q¤¸RiV=iTwU±Gn21ö]¬+ß:V

Test it

COBOL (GnuCOBOL), 317 294 279 270 269 249 248 247 bytes

Using go to statements: 247 bytes

data division.working-storage section. 1 i pic S99 value 99.procedure division.a.perform b 2 times display"Take one down and patch it around"compute i=4+i- random*21 set i to max(0,i)perform b display""if i>0 go to a.b.display i" bugs in the code"

Try it online!

Ungolfed

data division.                     <-- Define the variables we want to use
working-storage section.           <-- Define global variables used in this
                                       program

1 i pic S99 value 99.              <-- Define variable i with datatype signed
                                       numeric and with two digits

procedure division.                <-- Define our program

a.                                 <-- Define label a, to jump to
    perform b 2 times              <-- Execute the procedure called 'b'
                                       twice, see below
    display "Take one down and..." <-- Display this sentence
    compute i =
        4 + i - (random * 21)      <-- Subtract from i some random value
    set i to max(0,i)              <-- If i is negative, then assign 0 to i

    perform b                      <-- Execute the procedure 'b'
    display ""                     <-- Display an empty string; needed
                                       because of the blank line
    if i > 0                       <-- If i is still greater than 0, then
                                       we're not done yet
        go to a.                   <-- and we'll jump back to 'a'

b.                                 <-- Define procedure called 'b'.
    display i " bugs in the code"  <-- Display the value of i and then the
                                       given string literal

Using the perform statement as loop control: 269 bytes

data division.working-storage section. 1 i pic S99 value 99.procedure division.perform until i=0 perform a 2 times display"Take one down and patch it around"compute i=4+i-(random*21)if i<0 compute i=0 end-if perform a display""end-perform.a.display i" bugs in the code"

Try it online!

Ungolfed

data division.                     <-- Define the variables we want to use
working-storage section.           <-- Define global variables used in this
                                       program

1 i pic S99 value 99.              <-- Define variable i with datatype signed
                                       numeric and with two digits

procedure division.                <-- Define our program

perform until i = 0                <-- Perform the following code until i = 0
    perform a 2 times              <-- Execute the procedure called 'a' twice,
                                       see below

    display "Take one down and..." <-- Display this sentence
    compute i =
        4 + i - (random * 21)      <-- Subtract from i some random value

    if i < 0                       <-- if i < 0
        compute i=0                <-- then assign 0 to i
    end-if
    perform a                      <-- Execute the procedure 'a'
    display ""                     <-- Display an empty string; needed because
                                       of the blank line
end-perform.

a.                                 <-- Define procedure called 'a'.
    display i " bugs in the code"  <-- Display the value of i and then the
                                       given string literal

Note: the last sentence is still printed, because COBOL executes the whole program, and after the perform until loop it "falls-through" the label a, executing its statements. This behaviour is similar to a switch case without break.



PS: The numbers are not exactly displayed as required, but COBOL is not that good at auto-converting numbers to a pretty textual representation.

Julia 0.7, 123 115 bytes

~n="$n bugs in the code"
!(n=99,m=max(0,n-rand(-4:16)))=n>0?[~n;~n;"Take one down and patch it around";~m;"";!m]:~n

Try it online!

!() returns a vector of strings (for each line)

C,  169  165 bytes

Thanks to @ceilingcat for saving four bytes!

*s=" bugs in the code";f(i,j){for(i=99;i>0;i=j)printf("%d%s\n%d%s\nTake one down and patch it around\n%d%s\n\n",i,s,i,s,(j=i+rand()%19-15)<0?0:j,s);printf("0%s",s);}

Try it online!

PowerShell, 137 135 133 131 bytes

$b=' bugs in the code';for($a=99;$a){,"$a$b"*2;"Take one down and patch it around";$a+=-16..4|Random;if($a-lt0){$a=0}"$a$b`n"}"0$b"

Try it online!

The "bugs in the code" section is saved into $b for later use. Sets $a to 99, enters a for loop on $a. First we create an array of two strings ," "*2, with the string being the "X bugs in the code".

Next is just the string "Take one down and patch it around". Then we increment $a by selecting a Random integer from the range [-16,4]. After that, we clamp $a to be at minimum zero using an if if($a-lt0){$a=0}. Then the string "Y bugs in the code".

Finally, after the loop is finished, we put the string "0 bugs in the code" onto the pipeline. All of those strings are gathered from the pipeline, and an implicit Write-Output gives us newlines between them for free.

Saved two bytes using a for loop instead of a while loop.
Saved two bytes by moving $b onto its own section.
Saved two bytes thanks to Adrian Blackburn.

SAS 153 146 145 bytes

data;a=99;b='bugs in the code';do while(a>0);put a b/a b;put'Take one down and patch it around';a=max(a+int(ranuni(0)*20-16),0);put a b/;end;run;

The original version is Joe's answer, thanks him.

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

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

Python 3, 199 191 171 bytes

-8 bytes thanks to @Stephen
-20 bytes thanks to @Jo King

import random
x=99
l="bugs in the code\n"
p=print
while x>0:p(f"{x} {l}"*2+"Take one down and patch it around");x=max(0,x+random.choice(range(-16,5)));p(f"{x} {l}")
p(0,l)

Try it online!

Bash, 182 178 151 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.

LaTeX, 368 304 293 287 245 240 bytes

While not really competitive compared to the other programs in terms of bytes, I just wanted to see how to do this in LaTeX.

\documentclass{proc}\newcount\b\b99\usepackage[first=-16,last=5]{lcg}\def~{\the\b\ bugs in the code\\}\def\|{\loop~~Take one down and patch it around\\\rand\advance\b\value{rand}\ifnum\b>0 ~\\\repeat\b0 ~\\~}\begin{document}\|\end{document}

More readable:

\documentclass{proc}               %shortest document class
\newcount\b                        %short variable name
\b 99                              %set the value to 99
\usepackage[first=-16,last=5]{lcg} %random number generator
%\the prints the value of the variable behind it
%\def is shorter than \newcommand and can redefine commands
\def~{\the\b\ bugs in the code\\}
\def\|{                            %the function
    \loop
        ~
        ~
        Take one down and patch it around\\
        %\rand creates a counter named rand and                                        
        %stores the random value in it
        \rand \advance\b\value{rand} 
        %if the counter is smaller than 0, it becomes 0
        \ifnum\b>0 
            ~ \\                  %extra newline as required
    \repeat
    %if b is equal or smaller than 0, set it to 0
    \b 0 
    ~ \\                          %then print like normal
    %extra print at the end
    ~
}
\begin{document}
    \|                             %calling the function
\end{document}

Improvements (per edit):

  1. "x bugs in the code" is now a function instead of 4 lines
  2. Rewrote the \if clause for the \repeat as a \else
  3. Apparently \value{b}=x works for initialisation but not in the loop (instead of \setcounter{b}{x})
  4. Apparently \relax should be used for point 3, but that can also be achieved by inserting a space. Removed the \else, used TeX commands instead of LaTeX as these are shorter, and replaced \' by ~.
  5. Some code didn't need to be relaxed for some reason.

JavaScript (Node.js), 127 bytes

f=(i=99,t=` bugs in the code
`)=>i>0?i+t+`Take one down and patch it around
`+((i+=0|Math.random()*21-15)<0?0:i)+t+`
`+f(i):0+t

Try it online!


Explanation :

f = (                                          // start of our recursive function
    i=99,t=` bugs in the code                 // parameters, 1. num bugs , 2. text
`)                                           // using string literal end text with \n
=>                                          // start function
    i > 0 ?                                // if i is greater than 0
        i + t +                           // return value of i, value of t and  
    `Take one down and patch it around   // and this text 
    `                                   // + new line
    + (( i +=                          // and add 
        0|Math.random()*21-15) < 0 ?  // remove decimal from value obtained by multiplying
                                     // random number (between 0 and 1) multiplied by 21 
                                    // and then subtracting 15 from it
                                   // if that value is less than 0
        0 :                       // add 0 to i
        i                        // otherwise add value of i  
        )                       // end the parenthesis 
    + t                        // add value of t to that
    + `\n`                    // along with a new line 
    + f(i)                   // and repeat the process (this is recursive)
    :                       // if i > 0 condition not met then
        0 + t              // return 0 + t. Zero is there so the last line can be 
                          // repeated

Thanks to @tsh for the idea of recursion and implementation (saved some bytes)

Any suggestions to golf this further are welcome.

VBA: 212 163 Bytes

This solution is based on the one by Chronocidal posted yesterday. This my first post and I don't have enough reputation to comment on his post.

This revision contains two enhancements.

  1. Using While/Wend instead of For/Next saves a few characters.
  2. Calling a Sub named with a single character is shorter than GoSub and the Exit Sub and Return lines needed to support it.

Edit:
3. Removed whitespace and characters that the VBA editor will automatically add back in. See Tips for golfing in VBA
4. Added suggestions by @EricF, then saw his paste bin algorithm was even smaller so I replaced my algorithm with his and removed whitespace. A key change was appending vbLF to the output string so Debug.Print did not have to be called as often. Kudos to EricF.


Sub b()
s=" bugs in the code"&vbLf
c=99
While c
Debug.? c &s &c &s &"Take one down and patch it around
c=c+5-Int(Rnd()*20)
c=IIf(c<0,0,c)
Debug.? c &s
Wend
End Sub

This was a fun challenge. If you know of an online interpreter like TIO for VB6/VBScript/VBA please leave a comment.

If you want to test this code and have Microsoft Excel, Word, Access, or Outlook installed (Windows only), press Alt+F11 to open the VBA IDE. Insert a new code module (Alt+I,M) and clear out Option Explicit. Then paste in the code and press F5 to run it. The results should appear in the Immediate Window (press Ctrl+G if you don't see it).

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

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.

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}

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.

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;}

C (gcc), 141 137 bytes

I used preprocessor macros for great justice (warning: TV Tropes)

#define _(a)printf("%d bugs in the code\n"#a,i)
f(i){for(i=99;_(),i;i+=rand()%21-16,i*=i>0,_(\n))_(Take one down and patch it around\n);}

Try it online!

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!

Zsh, 133 bytes

b="%d bugs in the code
";for ((n=99;n;)){printf $b$b"Take one down and patch it around
$b
" n n n-=RANDOM%21-4,n=n\>0\?n:0};printf $b

Try it online!


This takes advantage of a few zsh features.


Ruby: 149 bytes

n=99;f=" bugs in the code\n";loop{puts"#{n}#{f}"*2+"Take one down and patch it around\n";(n+=rand -16..4)>0?puts("#{n}#{f}\n"):break};puts"0#{f}\n"*2

Should work in pretty much any version of Ruby >= 1.8

I think it might be possible to optimise the strings a bit more, but in general I'm pretty pleased with it - in particular the combo assignment/comparison/break statement and the abuse of optional brackets.

Note: the output technically has two trailing newlines; if that needs to be accounted for then it goes up by 4 characters.

Python 2,  138 134 133 131  127 bytes

-1 Thanks to Jo King (rearrange so as to use the logic bugs-=min(bugs,randomNumber) rather than bugs=max(0,bugs-randomNumber)). This allowed the force-quit using a division by zero error, saving a further 6 bytes!

r=n=99
t="bugs in the code\n"
while 1:r+=id(r);b=n;n-=min(n,r%21-4);print b,t,1/b|b,t,"Take one down and patch it around\n",n,t

Try it online!

Python 3, 156 138 bytes

Thanks to Jonathan's Python 2 answer for the id trick

r=n=99
z='%d bugs in the code\n'
while n:x=n;r+=id(r);n-=min(n,r%21-4);print((z%x)*2+'Take one down and patch it around\n'+z%n)
print(z%n)

Try it online!

Explanation:

r=n=99       #Initialise r and n to 99
z='%d bugs in the code\n'  #Save n
while n:     #Loop while n is not 0
    x=n      #Save value of n
    r+=id(r) #Modify value of r, which changes the hash value
    n-=min(n,r%21-4)  #Change n's value by a random number between 4 and -16
    print((z%x)*2+'Take one down and patch it around\n'+z%n)   #Print the message
print(z%n)  #And print the final line

VBA: 225 233 Bytes

Sub b()
For c = 99 To 0 Step -1
GoSub d
GoSub d
Debug.Print "Take one down and patch it around"
c = c + 5 - Int(rnd() * 20)
If c < 0 Then c = 0
GoSub d
Debug.Print
Next c
Exit Sub
d:
Debug.Print c & " bugs in the code"
Return
End Sub

{EDIT} Added the missing rnd()*

Notes:
Uses GoSub to print the triplicated line, because it's slightly shorter than assigning the line to a variable and Debug.Printing it.
Debug.Print without any arguments prints an empty line (no need for a Null or an empty string) A WorksheetFunction.Max line would be too long, so I used an "if less than" to prevent negatives.

 

With indentation and comments

Sub b()
    For c = 99 To 0 Step -1
        GoSub d '"# bugs in the code"
        GoSub d '"# bugs in the code"
        Debug.Print "Take one down and patch it around"
        c = c + 5 - Int(rnd() * 20)
        If c < 0 Then c = 0 'Non-negative
        GoSub d '"# bugs in the code"
        Debug.Print
    Next c
    Exit Sub
d: 'This is our GoSub bit
    Debug.Print c & " bugs in the code"
    Return
End Sub

C#, 184 181 bytes

My first Code Golf answer!

(Based on Java answer from @Kevin Cruijssen)

Func<String>c=()=>{String s="",t=" bugs in the code\n";for(int i=99;i>0;s+=i+t+i+t+"Take one down and patch it around\n"+((i-=new Random().Next(21)-4)<0?0:i)+t+"\n");return s+0+t;};

Try it online!

R, 182 140 138 135 bytes

n=99;while(n)cat(n,b<-" bugs in the code
",n,b,"take one down and patch it around
",n<-max(0,n-sample(21,1)+5),b,"
",c(n,b)[!n],sep="")

Try it online!

while being reasonably good at random number generation, R is terrible at strings and printing. JayCe found about a billion bytes, and continues to find new ways to golf this!

JavaScript, 189 176 168 162 bytes

f=(x=99)=>{c=console.log;m=Math;a=` bugs in the code
`;c(x+a+x+a+"Take one down and patch it around"+(x=m.max(x+m.ceil(m.random()*21-15),0))+a)
x?f(x):c(`
`+x+a)}

Try it online!

Thanks for Muhammad Salman for the missing console.log replacement, and for Oliver for the x test improvement

Thanks for l4m2 for golfing this by 8 bytes

Java 8, 161 160 bytes

v->{String s="",t=" bugs in the code\n";for(int i=99;i>0;s+=i+t+i+t+"Take one down and patch it around\n"+((i-=Math.random()*21+4)<0?0:i)+t+"\n");return s+0+t;}

-1 byte thanks to @JonathanAllan.

Try it online.

Explanation:

v->{                   // Method with empty unused parameter and String return-type
  String s="",         //  Result-String, starting empty
         t=" bugs in the code\n";
                       //  Temp-String we use multiple times
  for(int i=99;i>0;    //  Start `i` at 99, and loop as long as it's larger than 0
    s+=                //   Append to the result-String:
       i+t             //    The first line of the verse
       +i+t            //    The second (duplicated) line of the verse
       +"Take one down and patch it around\n"
                       //    The third line of the verse
       +((i-=Math.random()*21-4)
                       //    Decrease `i` by a random integer in the range [-16, 4]
         <0?0:i)       //    If `i` is now negative, set it to 0
        +t+"\n");      //    And append the fourth line of the verse
  return s             //  Return the result,
         +0+t;}        //  appended with an additional line for 0 at the very end

Perl, 132 bytes

$c=" bugs in the code
";for($i=99;$i>0;$i+=~~rand(21)-16){print$t.$/,($t=$i.$c)x2,"Take one down and patch it around
"}print"0$c
"x2

Jelly, 61 bytes

;“,Ȥm46Ṛṛ⁽eɼḞF»
ÇȮ“"ḃḲɠ⁼ċTṪʠ/Ạ⁺ṗḍ^ẸƘⱮṖ8»20X+_«¥©17Ç⁷®ßÇ®?
99Ç

A niladic link which also works as a full program.

Try it online! (output is flushed after execution is complete but it prints paragraph by paragraph)

How?

;“,Ȥm46Ṛṛ⁽eɼḞF» - Link 1, helper to construct count-text: number
 “,Ȥm46Ṛṛ⁽eɼḞF» - compressed string (list of characters) = " bugs in the code\n"
;               - concatenate the number with that list of characters

ÇȮ“"ḃḲɠ⁼ċTṪʠ/Ạ⁺ṗḍ^ẸƘⱮṖ8»20X+_«¥©17Ç⁷®ßÇ®? - Link 2, print stuff: number
Ç                                         - call the last Link (1) as a monad
 Ȯ                                        - print and yield that
                                          - ...at this point that is then printed again
                                          -    implicitly due to the start of a new leading
                                          -    constant chain below
  “"ḃḲɠ⁼ċTṪʠ/Ạ⁺ṗḍ^ẸƘⱮṖ8»                  - compressed string (list of characters)
                                          -     = "Take one down and patch it around\n"
                                          - ...once again an implicit print, same reason
                        20                - twenty
                          X               - random int from [1,20] 
                           +              - add the left argument, the number
                                17        - seventeen
                              ¥           - last two links as a dyad:
                             «            -   minimum (of rand[1,20]+n and 17) 
                            _             -   subtract
                               ©          - copy this newNumber to the register
                                  Ç       - call last Link (1) as a monad = f(newNumber)
                                          - here we get another implicit print, same reason
                                   ⁷      - a new line character
                                          - yet another implicit print, same reason
                                    ®     - recall newNumber from the register
                                        ? - if... 
                                       ®  - ...condition: recall from register again
                                          -               (i.e. if non-zero)
                                     ß    - ...then: call this Link (2) as a monad
                                          -          = Link 2(newNumber)
                                      Ç   - ...else: call the last Link (1) as a monad
                                          -          = Link 1(0) (since newNumber=0)

99Ç - Main Link: no arguments
99  - yep, you guessed it, ninety nine
  Ç - call the last Link (2) as a monad

Clean, 245 234 bytes

import StdEnv,Math.Random,System.Time,System._Unsafe,Text
f[n,v:l]b|n>0=n<+b<+n<+b+"Take one down and patch it around\n"<+max 0v<+b+"\n"+f[v:l]b=0<+b

f(scan(+)99[n rem 20-16\\n<-genRandInt(toInt(accUnsafe time))])" bugs in the code\n"

Try it online!

.COM opcode, 137 bytes

0000h: BE 63 00 E8 3B 00 85 F6 75 01 C3 E8 33 00 B2 61
0010h: CD 21 0F C7 F0 F7 26 87 01 01 D6 83 EE 10 73 02
0020h: 31 F6 E8 1C 00 B2 5E CD 21 EB D8 99 F7 36 85 01
0030h: 85 C0 74 05 52 E8 F3 FF 5A B4 02 80 CA 30 CD 21
0040h: C3 89 F0 E8 E5 FF B4 09 BA 4D 01 EB F1 20 62 75
0050h: 67 73 20 69 6E 20 74 68 65 20 63 6F 64 65 0D 0A
0060h: 24 54 61 6B 65 20 6F 6E 65 20 64 6F 77 6E 20 61
0070h: 6E 64 20 70 61 74 63 68 20 69 74 20 61 72 6F 75
0080h: 6E 64 0D 0A 24 0A 00 15 00                     

        org 100h

        mov si, 99
lp:     call put2
        test si, si
        jnz @f
            ret
        @@:
        call put2
        mov dl, str2 - 256
        int 33
        rdrand ax
        ;mov ax, 65536*14/21+1
        mul [twentyone]
        add si, dx
        sub si, 16
        ;lea si, [esi+edx-16]
        jnc @f
            xor si, si
        @@:
        call put2
        mov dl, str2 - 256 - 3
        int 33
        jmp lp

put1:   cwd
        div [ten]
        test ax, ax
        jz @f
            push dx
            call put1
            pop dx
        @@:
        mov ah, 2
        or dl, 48
int33:  int 33
        ret

put2:   mov ax, si
        call put1
        mov ah, 9
        mov dx, str1
        jmp int33

str1:   db ' bugs in the code', 13, 10, '$'
str2:   db 'Take one down and patch it around', 13, 10, '$'
ten     dw 10
twentyone dw 21

Pyth, 94 92 bytes

J99WJ%jb[K"%d bugs in the code"K"Take one down and patch it around"Kk)[JJ=JeS,Z+J-O21 16;%KZ

Try it online


Previous version: 94 bytes

J99WJp%jb[K"%d bugs in the code"K"Take one down and patch it around"K)[JJ=JeS,Z+J-O21 16)b;%KZ

ABAP, 295 bytes

...because why the heck not!

REPORT z.DATA:a(16),c TYPE qfranint.a = 'bugs in the code'.data(b) = 99.WRITE:/ b,a.WHILE b > 0.WRITE:/ b,a,/'Take one down and patch it around'.CALL FUNCTION
'QF05_RANDOM_INTEGER' EXPORTING ran_int_max = 21 IMPORTING ran_int = c.b = b + c - 17.IF b < 1.b = 0.ENDIF.WRITE:/ b,a,/,/ b,a.ENDWHILE.

It's certainly not competitive compared to other languages, but I even managed to slim it down from 330 bytes I wrote initially, so I count it as a personal win.

Since ABAP doesn't allow lines longer than 255 characters, I had to replace a space with a line break. On Windows this initially increased the size to 296 bytes due to CRLF, but it runs fine with just the LF in there. ABAP sure requires many spaces though, so this is no big deal.

WRITE simply dumps text the GUI, so I guess that is kind of like stdout? I could probably save some bytes here by using a structure or table, but due to how SAP handles mixed structures (containing chars and numbers) the approach I imagined would only work on non-Unicode systems... Which I personally consider a no-go, despite having access to both.

The function module for random numbers is the only one I could find in our system, I suppose there could be one with a shorter name or parameters. No idea!

More or less readable code, comments included:

REPORT z.
  "Define a (text) and c (random)
  DATA: a(16),
        c TYPE qfranint. "A stupid data type for a random INT

  "This is shorter than using VALUE (saves 3 bytes)
  a = 'bugs in the code'.
  "This is slightly shorter than doing ',b TYPE i' and 'b = 99'. (saves 2 bytes)
  data(b) = 99.

  "first line has to be outside of loop due to our exit condition (saved me ~5 bytes)
  WRITE: / b,a. "\n xx bugs in the code

  WHILE b > 0.
    WRITE: / b,a, "\n xx bugs in the code
    /'Take one down and patch it around'.

    "This ridiculous function for random numbers...
    "To save some bytes, I leave ran_int_min at it's default
    "of 1, and set the max to 21 instead, from which I remove
    "17 later on, resulting in a range of [-16,4]
    "Compare:
    "   ' - 17'              =  5 bytes
    "   ' ran_int_min = -16' = 18 bytes
    "(saves 13 bytes)

    CALL FUNCTION 'QF05_RANDOM_INTEGER'
        EXPORTING ran_int_max = 21
        IMPORTING ran_int = c.

    "Maximum number of bugs added:     4 = 21 - 17
    "Maximum number of bugs removed: -16 =  1 - 17
    b = b + c - 17.

    IF b <= 0.
        b = 0.
    ENDIF.
     
    WRITE: / b,a,/,/ b,a. "\nxx bugs in the code\n\nxx bugs in the code
  ENDWHILE.

Thanks for the challenge!
To my boss: Please don't fire me, I'm just educating myself!

Japt, 124 123 121 120 116 110 107 bytes

U=U||99P=` Þï  e ¬¸`OpU+POpU+P  Op`Take e ܵ, p®  ÂÐ` U=Mr0-15,5 +U-1U=Uc  (U>0 ?(OpU+P+R ,ßU  :Oo0+P

Try it online!

Red, 166 163 bytes

b: 99 forever[if b > 1[print[b t:{bugs in the code}s:"^/"b t
s{Take one down and patch it around}]if
0 > b: b + 4 - random 20[prin[0 t s s 0 t]break]print[b t s]]]

Try it online!

More readable:

b: 99
t: {bugs in the code}
s: "^/" 
forever [
    if b > 1 [
        print [ b t s b t s {Take one down and patch it around} ]
        if 0 > b: b + 4 - random 20 [
            prin [ 0 t s s 0 t ]
            break
        ]
        print [ b t s ]
    ]
]

Charcoal, 81 bytes

≔⁹⁹θ≔”⧴"pWº⁴tSn/{yBⅈ⁺”ζW›θ⁰«×²﹪ζθ”↶0⪫\`3+¤⸿Zν(z¬hLÀTj'ZXεPraF”≧⁺⁻⁴‽²¹θ﹪ζ×θ›θ⁰¶»﹪ζ⁰

Try it online! Link is to verbose version of code. Explanation:

≔⁹⁹θ

Start with 99 bugs in the code.

≔”⧴"pWº⁴tSn/{yBⅈ⁺”ζ

Save the compressed string "%d bugs in the code\n".

W›θ⁰«

Repeat while there are a positive number of bugs remaining.

ײ﹪ζθ

Print the number of bugs in the code twice.

”↶0⪫\`3+¤⸿Zν(z¬hLÀTj'ZXεPraF”

Print "Take one down and patch it around".

≧⁺⁻⁴‽²¹θ

Add a random number of bugs between -17 (exclusive) and 4 (inclusive).

﹪ζ×θ›θ⁰

Print the number of bugs remaining, or 0 if negative.

Leave a blank line between verses.

»﹪ζ⁰

After the last verse, print 0 bugs in the code again.

QB64, 134 bytes

From my brother.

S$="bugs in the code":I=99:WHILE I>0:?I;S$:?I;S$:?"Take one down and patch it around":I=I+INT(RND*21)-15:I=-(I>0)*I:?I;S$:?:WEND:?I;S$

JavaScript, 138 bytes

_=>(I=i=>`Take one down and patch it around
${l=(i>0?i:0)+` bugs in the code
`}
`+l+l+(i>0&&I(i+Math.random()*21-15|0)))(99).slice(55,-25)

f=

_=>(I=i=>`Take one down and patch it around
${l=(i>0?i:0)+` bugs in the code
`}
`+l+l+(i>0&&I(i+Math.random()*21-15|0)))(99).slice(55,-25)

console.log(f())

PHP, 126 bytes

Run on the command line, using php -r 'code here':

$b=" bugs in the code
";for($x=99;print$x.$b,$x;)echo"$x{$b}Take one down and patch it around
",$x-=min($x,rand(-4,16)),"$b
";

T-SQL, 188 bytes

DECLARE @ INT=99,@b CHAR(18)=' bugs in the code
'a:PRINT CONCAT(@,@b,@,@b,'Take one down and patch it around
')SET @+=5-22*RAND()IF @<0SET @=0PRINT CONCAT(@,@b,'
')IF @>0GOTO a;PRINT '0'+@b

SQL allows returns inside string literals, so that helps.

CONCAT() does an implicit conversion to text so I don't have to worry about CAST or CONVERT.

SAS, 210 bytes

%macro t;%let b=bugs in the code;%let a=99;%do%until(&a<=0);%put&a &b;%put&a &b;%put Take one down, pass it around;%let a=%eval(&a-%sysfunc(ranbin(0,20,.3))+4);%if &a<0%then%let a=0;%put&a &b;%put;%end;%mend;%t

Ungolfed:

%macro t;
%let b=bugs in the code;
%let a=99;
%do%until(&a<=0);
  %put &a &b;
  %put &a &b;
  %put Take one down, pass it around;    
  %let a=%eval(&a-%sysfunc(ranbin(0,20,.3))+4);
  %if &a<0%then%let a=0;
  %put &a &b; 
  %put;
%end;
%mend;
%t

Can save a few bytes if warnings in the log are permitted (put the &a into the &b macro variable, but that generates an initial warning).

Python 3, 144 bytes

from pylab import*
s='bugs in the code\n'
X=99
while X>0:i=randint(-16,5);print(X,s,X,s,'\ntake one down and patch it around',max(X+i,0),s);X+=i

Octave, 149 148 bytes

Saved one byte by changing randi(21) and %i to 21*rand and %.f. %.f ensures the output is a float with zero decimals (i.e. and integer).

Inserted a bunch of line breaks instead of commas and semicolons to ease readability. It feels wrong, but it's not longer than the one-liner.

x=99;do(p=@(i)printf('%.f bugs in the code\n',i))(x)
p(x)
disp('Take one down and patch it around')
p(max([0,x+=21*rand-17]))
disp('')until x<1
p(0)

Try it online!

Explanation:

x=99;               % Initialize the number of bugs to 99
do ...  until x<1   % Loop until the number of bugs is less than 1
 (p=@(i)printf('%.f bugs in the code\n',i))(x)  % Create a function handle p that takes
                                                % the number of bugs as input and outputs
                                                % the string x bugs ... \n
 p(x)                % Call that function with the same number of bugs to get two lines
 ,disp('Take on down and patch it around')       % Outputs that line, including a newline
 ,p(max([0,x+=21*rand-17]))                    % Call p again, while updating the number
                                                 % of bugs. The number of bugs will be 
                                                 % the old one plus the random number, or 0
                                                 % if it would have turned negative
 ,disp('')        % A newline
p(0)              % Finally, the last single line.

Using p((x+=21*rand-17)*(x>0) instead of max saves a byte, but the last line outputs -0 bugs ... instead of 0 bugs. It works with randi(21)-17, but then it's the same length as the one above. Try it online!

Python 2, 151 bytes

Nice trick j=i+max(-i,randint(-16,4)) by Jo King, exploiting allowed uneven distribution

Saved couple bytes thanks to Mnemonic

from random import*
i,s=99,'bugs in the code\n'
while i:j=max(0,i+randint(-16,4));print i,s,i,s,'Take one down and patch it around\n',j,s;i=j
print i,s

Try it online!

05AB1E, 59 bytes

99[Ð16(4ŸΩ+¾‚{θ©3F“ÿ±À€†€€ƒË“Š}“ƒ¶€µ„‹€ƒš¡€•…¡“ªsõ®>#®]sDŠ»

Try it online!