[Question]
  [
Your task is to make a program that takes in an integer `n > 1`, and outputs the roll of a single `n`-sided die. However, this dice follows the rules for [exploding dice](https://anydice.com/articles/exploding-dice/).
When you roll the die, check what value you rolled. If you got the maximum for that kind of die (on a standard d4 that would be 4, or 6 on a d6, etc.), roll again and add the new roll to that total. Each roll continues adding to the total, until you don't roll the max number anymore. That final number is still added though.
Your program should take in a single integer `n`, and roll the exploding `n`-sided die. [Here's an example distribution to show what it should look like for `n=4`](https://paste.ee/p/FKVN4)[.](https://www.youtube.com/watch?v=dQw4w9WgXcQ) Note that you should never output any multiples of `n`, since they will always explode. 
You can assume the stack size for any recursion you do is infinite, and your random function [must meet our standards for randomness](https://codegolf.meta.stackexchange.com/questions/1324/standard-definitions-of-terms-within-specifications/1325#1325) (built-in random generator or [time/date](https://codegolf.meta.stackexchange.com/questions/13029/is-the-current-time-or-date-with-modulo-random-enough)). Your random function should also be as uniform as possible, vs. something like a geometric distribution, since these are dice we're talking about.
  
[Answer]
# x86 Machine Code (for Intel Ivy Bridge and later), 17 bytes
```
31 C9 0F C7 F0 31 D2 F7 F6 42 01 D1 39 F2 74 F2 C3
```
The above bytes of code define a function that simulates an exploding die. It takes a single input, passed in the `ESI` register, indicating the maximum number of the die. It returns a single value in the `ECX` register, which is the result of the rolls.
Internally, it uses [the `RDRAND` instruction](https://en.wikipedia.org/wiki/RdRand) to generate a random number. This uses a random number generator (RNG) that is built into the hardware on Intel Ivy Bridge processors and later (some AMD CPUs also support this instruction).
The logic of the function is otherwise quite straightforward. The generated random number is scaled to lie within the desired range using the standard technique (`(rand % dieSize) + 1`), and then it is checked to see if it should cause an explosion. The final result is kept in an accumulator register.
Here is an annotated version showing the assembly language mnemonics:
```
           unsigned int RollExplodingDie(unsigned int dieSize)
31 C9        xor     ecx, ecx    ; zero-out ECX, which accumulates the final result
           Roll:
0F C7 F0     rdrand  eax         ; generate a random number in EAX
31 D2        xor     edx, edx    ; zero-out EDX (in preparation for unsigned division)
F7 F6        div     esi         ; divide EDX:EAX by ESI (the die size)
                                 ;   EAX receives the quotient; EDX receives the remainder
42           inc     edx         ; increment the remainder
01 D1        add     ecx, edx    ; add this roll result to the accumulator
39 F2        cmp     edx, esi    ; see if this roll result should cause an explosion
74 F2        jz      Roll        ; if so, re-roll; otherwise, fall through
C3           ret                 ; return, with result in ECX register
```
I am cheating *a bit*. All standard x86 calling conventions return a function's result in the `EAX` register. But, in true machine code, there are no calling conventions. You can use any registers you want for input/output. Using `ECX` for the output register saved me 1 byte. If you want to use `EAX`, insert a 1-byte `XCHG eax, ecx` instruction immediately before the `ret` instruction. This swaps the values of the `EAX` and `ECX` registers, effectively copying the result from `ECX` into `EAX`, and trashing `ECX` with the old value of `EAX`.
**[Try it online!](https://tio.run/##dVNba8IwFH7vrzhUhHa6G9uTusEuHfRhDqyDMYRSk9idEdOSpEMm/vV1idWOancemvZ833dybiWnKSFlWQiFqWAUUGiYZJwHq5xnFEX6iMxroBRZhN/Md9YOGKsxyVTB9XDrjONELePYc2d6lUluPN0uI6t@dcyEu2XtzZ1m@eDIOdOSykRQq0lWLfBfZFpFpm0sil87lsIWGAWpg7TACaXNO0gbiyzz@o7/M/n83r6ZYg/AAbg3xAWv6qAPcH4CWaHzQpumpqg0k4AKgoc3ODk/EEYugLcfiRWisLqmMAqPhTZJt3YYYa0gPJvPzcnNJxwI/Wq@kulCinrkm7Jj@1hQBiOlKcf52cet0/BhZl1Oh7IFCgaPYRBH4XsATbuuCePX5/tgEr88xeE0mNxNw5dxZAiXF45jl3CZoPB2K7jIJHjK1B9rQLiBi6E5Rq0RhtDroW9F611VuTThFp5rdx66V3Qw0@ZpBtQHM8qjP2Gft181YmNK/yELnqSqPF2afRX0Fw "C (gcc) – Try It Online")**
Here's the equivalent function transcribed in C, using [the `__builtin_ia32_rdrand32_step` intrinsic](https://gcc.gnu.org/onlinedocs/gcc/x86-Built-in-Functions.html) supported by GCC, Clang, and ICC to generate the `RDRAND` instruction:
```
#include <immintrin.h>
unsigned int RollExplodingDie(unsigned int dieSize)
{
    unsigned int result = 0;
Roll:
    unsigned int roll;
    __builtin_ia32_rdrand32_step(&roll);
    roll    = ((roll % dieSize) + 1);
    result += roll;
    if (roll == dieSize)   goto Roll;
    return result;
}
```
Interestingly, [GCC with the `-Os` flag transforms this into almost exactly the same machine code](https://gcc.godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(fontScale:0.8957951999999999,j:1,lang:c%2B%2B,source:'%23include+%3Cimmintrin.h%3E%0A%0Aunsigned+int+RollExplodingDie(unsigned+int+dieSize)%0A%7B%0A++++unsigned+int+result+%3D+0%3B%0ARoll:%0A++++unsigned+int+roll%3B%0A++++__builtin_ia32_rdrand32_step(%26roll)%3B%0A++++roll++++%3D+((roll+%25+dieSize)+%2B+1)%3B%0A++++result+%2B%3D+roll%3B%0A++++if+(roll+%3D%3D+dieSize)+++goto+Roll%3B%0A++++return+result%3B%0A%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:48.91472868217055,l:'4',m:100,n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:g83,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'1',trim:'1'),fontScale:0.8957951999999999,lang:c%2B%2B,libs:!(),options:'-m64+-mrdrnd+-Os',source:1),l:'5',n:'0',o:'x86-64+gcc+8.3+(Editor+%231,+Compiler+%231)+C%2B%2B',t:'0')),header:(),k:51.08527131782946,l:'4',m:100,n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4). It takes the input in `EDI` instead of `ESI`, which is completely arbitrary and changes nothing of substance about the code. It must return the result in `EAX`, as I mentioned earlier, and it uses the more efficient (but larger) `MOV` instruction to do this immediately before the `RET`. Otherwise, samezies. It's always fun when the process is fully reversible: write the code in assembly, transcribe it into C, run it through a C compiler, and get your original assembly back out!
[Answer]
# [Python 2](https://docs.python.org/2/), ~~66~~ ~~64~~ 61 bytes
-3 bytes thanks to xnor
```
f=lambda n,c=0:c%n or c+f(n,randint(1,n))
from random import*
```
[Try it online!](https://tio.run/##PYpBCsMgFAXX8RRvE9DWRRuyKKEe5sdUIsSvfITQ09ski64GZqZ865p5aC24jdK8ENh695h8z8gCfw@arRAvkat@WjZGBckJpzoQU8lSb41c0KNRRc6NjNrXuH1AeOM1qY7gcPXuP7Qf "Python 2 – Try It Online")
The previous roll is stored in `c`, allowing us to access it multiple times without having to store it to a variable, which can't be done in a Python lambda. Each recursion, we check if we rolled exploding dice.
`c` is initialised to zero, so `c%n` is falsey there. In the next iterations, it will only be falsey if exploding dice were rolled.
# [Python 2](https://docs.python.org/2/), 55 bytes
```
f=lambda n:randint(1,n)%n or n+f(n)
from random import*
```
[Try it online!](https://tio.run/##PYtBCsMgFAXXeoq3CWibTUIWIdTD/GJFIX7lI4Sc3jZZdDWLmalni4Xn3oPbKb89gTch9ombmUa2A6MI@BkMWx2kZFz2h5Rrkfbo5IJZrK5yHWT1EdP@AeGFddOK4HB79Q/6Fw "Python 2 – Try It Online")
My other answer seems to be a bit overengineered, since this appears to work as well... I'll leave it anyway.
[Answer]
# [R](https://www.r-project.org/), 39 bytes
```
n=scan();n*rgeom(1,1-1/n)+sample(n-1,1)
```
[Try it online!](https://tio.run/##RchBCoAgEADAe6/ouFtKCHUKH2OyhmBbqEEQvd3s1HEmFqfdyTb7neESyRr@jRfehfWXgDN3caV9AyWUVANjn8x2BAKWdbA8TTZLZaQjeGsygaJJtA5GRGzKCw "R – Try It Online")
Explanation: this solution avoids recursion/while loops by directly calculating the distribution of the number of explosions that will occur. Let \$n\$ be the number of sides on the die. If you denote success as rolling an \$n\$ and failure as rolling anything else, then you have probability \$\frac1n\$ of success. The total number of explosions is the number of successes before the first failure. This corresponds to a \$\mathrm{Geometric}\left(1-\frac{1}{n}\right)\$ distribution (see the [wikipedia page](https://en.wikipedia.org/wiki/Geometric_distribution), which defines success and failure the other way round). Each explosion brings \$n\$ to the total. The final roll follows a \$\mathrm{Uniform}(1,2,\ldots,n-1)\$ distribution which we add to the total.
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 26 bytes
```
{sum {roll 1..$_:}...*-$_}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/urg0V6G6KD8nR8FQT08l3qpWT09PS1clvvZ/cWKlQpqGiaZCWn6RQpyhgfV/AA "Perl 6 – Try It Online")
### Explanation
```
{                        } # Anonymous block
                  ...      # Sequence constructor
     {roll 1..$_:}         #   Next elem is random int between 1 and n
                           #   (Called as 0-ary function with the original
                           #   $_ for the 1st elem, then as 1-ary function
                           #   with $_ set to the previous elem which
                           #   equals n.)
                     *-$_  #   Until elem not equal to n (non-zero difference)
 sum                       # Sum all elements
```
[Answer]
# [J](http://jsoftware.com/), ~~16~~ 11 bytes
```
(+$:)^:=1+?
```
[Try it online!](https://tio.run/##y/qvpKegnqZga6WgrqCjYKBgBcS6egrOQT5u/zW0Vaw046xsDbXt/2tyWSpaGSrY6GnrmylaGSioq3OVJIO0GRopK2krmCiYKphxpSZn5Cto6KRpAkVKkv8DAA "J – Try It Online")
## Explanation
**TL;DR** `1+?` performs the die roll, `(+$:)^:=` reiterates only when it equals the input.
---
The function is a train of 4 verbs:
```
             ┌─ + 
         ┌───┴─ $:
  ┌─ ^: ─┴─ =     
  │               
──┤      ┌─ 1     
  └──────┼─ +     
         └─ ?     
```
A train is when 2 or more verbs are concatenated. Here, the answer is of the form `f g h j`:
```
(+$:)^:=  1  +  ?
    f     g  h  j
```
A so-called "4-train" is parsed as a hook and a fork:
```
f g h j   ⇔   f (g h j)
```
Thus, the answer is equivalent to:
```
(+$:)^:= (1 + ?)
```
### Hooks: `(f g) x` and `x (f g) y`
A monadic (one-argument) hook of two verbs, given an argument `x`, the following equivalence holds:
```
(f g) x   ⇔   x f (g x)
```
For example, `(* -) 5` evaluates to `5 * (- 5)`, which evaluates to `_25`.
This means that our 4-train, a hook of `f` and `(g h j)`, is equivalent to:
```
(f (g h j)) x   ⇔   x f ((g h j) x)
```
But what does `f` do here? `(+$:)^:=` is a conjunction of two verbs using the **Power** conjunction `^:`: another hook (`(+$:)`) and a verb (`=`). Note here that `f` is *dyadic*—it has two arguments (`x` and `(g h j) x`). So we have to look at how `^:` behaves. The power conjunction `f^:o` takes a verb `f` and either a verb or a noun `o` (a noun is just a piece of data) and applies `f` `o` times. For example, take `o = 3`. The following equivalences holds:
```
(f^:3) x     ⇔   f (f (f x))
x (f^:3) y   ⇔   x f (x f (x f y))
```
If `o` is a verb, the power conjunction will simply evaluate `o` over the arguments and use the noun result as the repeat count.
For our verb, `o` is `=`, the equality verb. It evaluates to `0` for differing arguments and to `1` for equal arguments. We repeat the hook `(+$:)` once for equal arguments and no times for differing ones. For ease of notation for the explanation, let `y ⇔ ((g h j) x)`. Remember that our initial hook is equivalent to this:
```
x   (+$:)^:=   ((g h j) x)
x   (+$:)^:=   y
```
Expanding the conjunction, this becomes:
```
x ((+$:)^:(x = y)) y
```
If `x` and `y` are the same, this becomes:
```
x (+$:)^:1 y   ⇔   x (+$:) y
```
Otherwise, this becomes:
```
x (+$:)^:0 y   ⇔   y
```
Now, we've seen monadic forks. Here, we have a dyadic fork:
```
x (f g) y   ⇔   x f (g y)
```
So, when `x` and `y` are the same, we get:
```
x (+$:) y   ⇔   x + ($: y)
```
What is `$:`? It refers to the entire verb itself and allows for recursion. This means that, when `x` and y`are the same, we apply the verb to`y`and add`x` to it.
### Forks: `(g h j) x`
Now, what does the inner fork do? This was `y` in our last example. For a monadic fork of three verbs, given an argument `x`, the following equivalence hold:
```
(g h j) x   ⇔   (g x) h (j x)
```
For this next example, suppose we have verbs named `SUM`, `DIVIDE`, and `LENGTH`, which do what you suppose they might. If we concatenate the three into a fork, we get:
```
(SUM DIVIDE LENGTH) x   ⇔   (SUM x) DIVIDE (LENGTH x)
```
This fork evaluates to the average of `x` (assuming `x` is a list of numbers). In J, we'd actually write this as example as `+/ % #`.
One last thing about forks. When the leftmost "tine" (in our symbolic case above, `g`) is a noun, it is treated as a constant function returning that value.
With all this in place, we can now understand the above fork:
```
(1 + ?) x   ⇔   (1 x) + (? x)
            ⇔   1 + (? x)
```
`?` here gives a random integer in the range \$[0,x)\$, so we need to transform the range to represent dice; incrementing yields the range \$[1, x]\$.
### Putting it all together
Given all these things, our verb is equivalent to:
```
((+$:)^:=1+?) x   ⇔   ((+$:)^:= 1 + ?) x
                  ⇔   ((+$:)^:= (1 + ?)) x
                  ⇔   x ((+$:)^:=) (1 + ?) x
                  ⇔   x ((+$:)^:=) (1 + (? x))
                  ⇔   x (+$:)^:(x = (1 + (? x))
(let y = 1 + (? x))
if x = y          ⇒   x + $: y
otherwise         ⇒   y
```
This expresses the desired functionality.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
X+ß}¥=¡
```
[Try it online!](https://tio.run/##y0rNyan8/z9C@/D82kNLbQ8t/P//vxkA "Jelly – Try It Online")
Uses recursion. Runs the program again (`ß`) and adds (`+`) if (`¡`) the random number (`X`) is equal (`=`) to the program input. `}` makes `ß` act on the program input and `¥` combines `+ß}` into a single link for `¡` to consume.
Here a distribution of 1000 outputs for n=6 which I collected using [this](https://tio.run/##AScA2P9qZWxsef//WCvDn33CpT3Cof874bi3L8OHJCQxMDAwwqHhuIr//zY) program. Plotted with python/matplotlib.
[![histogram](https://i.stack.imgur.com/a2GNL.png)](https://i.stack.imgur.com/a2GNL.png)
Here is a 5000 data points from n=3 on a semilog plot which shows the (approximately?) exponential distribution.
[![enter image description here](https://i.stack.imgur.com/OgiYK.png)](https://i.stack.imgur.com/OgiYK.png)
[Answer]
# Pyth - ~~12~~ 11 bytes
Uses functional while. I feel like there should be a smarter answer that just simulates the distribution.
```
-.W!%HQ+hOQ
-         (Q)         Subtract Q. This is because we start Z at Q to save a char
 .W                   While, functionally
  !                   Logical not. In this case, it checks for 0
   %HQ                Current val mod input
  +     (Z)           Add to current val
   h                  Plus 1
    OQ                Random val in [0, input)
```
[Try it online](http://pyth.herokuapp.com/?code=-.W%21%25HQ%2BhOQ&test_suite=1&test_suite_input=4%0A6&debug=0).
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 12 bytes
It may be shorter than dana's solution, but it's a hell of a lot uglier. I'm only posting it 'cause it seems like forever since we had a Japt solution that started with an empty line.
```
ö
>°V©VªV+ß
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=CvYKPrBWqVaqVivf&input=Ng)
[Answer]
# [Python 3](https://docs.python.org/3/), 80 bytes
```
import random as r,math
lambda n:int(-math.log(r.random(),n))*n+r.randint(1,n-1)
```
[Try it online!](https://tio.run/##JcoxCsAgDADAva9wNK0K0q2/SRGqYBKxLn19irge176RhU/VQk36MB05CRl8TXeEI28V6U5o@Co8rJ8Uqjy2hzUtOAbY@VgwU3TsI6j@ "Python 3 – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 10 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
[ILΩDIÊ#}O
```
[Try it online](https://tio.run/##yy9OTMpM/f8/2tPn3EoXz8NdyrX@//8bAwA) or [verify the lists](https://tio.run/##yy9OTMpM/f8/2tPn3EoXz8NdyrWatv46//8bAwA).
**10 bytes alternative:**
```
[LΩDˆÊ#}¯O
```
[Try it online](https://tio.run/##yy9OTMpM/f8/2ufcSpfTbYe7lGsPrff//98YAA) or [verify the lists](https://tio.run/##yy9OTMpM/f8/2ufcSpfTbYe7lGsPrbf11/n/3xgA).
Although I like the top one more because it got the 'word' `DIÊ` in it, which suits the challenge.
**Explanation:**
```
[         # Start an infinite loop:
 IL       #  Create a list in the range [1, input]
   Ω      #  Pop and push a random value from this list
    D     #  Duplicate it
     IÊ   #  If it's NOT equal to the input:
       #  #   Stop the infinite loop
}O        # After the loop: sum all values on the stack
          # (which is output implicitly as result)
[         # Start an infinite loop
 L        #  Create a list in the range [1, (implicit) input]
  Ω       #  Pop and push a random value from this list
   Dˆ     #  Add a copy to the global_array
     Ê    #  If it's NOT equal to the (implicit) input:
      #   #   Stop the infinite loop
}¯        # After the loop: push the global_array
  O       # Pop and push its sum
          # (which is output implicitly as result)  
```
[Answer]
# JavaScript (ES6), ~~39~~ 30 bytes
*Saved 9 bytes thanks to @tsh!*
```
f=n=>Math.random()*n|0||n+f(n)
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNHNy09J/f8/zTbP1s43sSRDrygxLyU/V0NTK6/GoKYmTztNI0/zf3J@XnF@TqpeTn66RpqGmabmfwA "JavaScript (Node.js) – Try It Online") or [See the distribution for n=4](https://tio.run/##NYxBDsIgEEX3nGKWILapia4Qb@AJmi5IC1pTZwwQN6W9OqKNq//y8/Ie5m1C78dXrJAGm7PTqC9XE@@1NzjQk4sdpiYllI6jyI48D9FE0DAvexij9QUbtdEZDva0sZQCZgbwlVtvQ7EcP4qubLX@z06xhfWEgSZbT3T7pYXKHw)
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 50 bytes
```
R@#//.x_/;x~Mod~#==0:>x+R@#&
R=RandomChoice@*Range
```
[Try it online!](https://tio.run/##RY1NC4IwHMbvfoo/Ch1Kml06FItR0C0I8zZGLJ1uMB1sg/TiV1/TS8/t@fG89NxL0XOvah46HEqSIbQf3@g8zg/TzBnGxeky7iLfJCUu@dCY/iaNqgXZRteJ8LRq8AQ6emSwKElWQlMprADlIB6A51pPYFo4FFERWfN1KfvHyctYf51otQRJxT9a0GUyXwssvyvrPAvhBw "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [R](https://www.r-project.org/), ~~47~~ 42 bytes
```
function(n){while(!F%%n)F=F+sample(n,1)
F}
```
[Try it online!](https://tio.run/##K/qfqmCj@z@tNC@5JDM/TyNPs7o8IzMnVUPRTVU1T9PN1k27ODG3ACiQp2OoyeVW@7@kKDMxpxioScHQINWYi6skMQkoW5RakJOZnFiSqgGR10nVMNHU1NSH8P4DAA "R – Try It Online")
Credit to [ArBo's approach](https://codegolf.stackexchange.com/a/183091/67312).
Still a byte longer than [Robin Ryder's](https://codegolf.stackexchange.com/a/183087/67312), go upvote his!
[Answer]
# [Ruby](https://www.ruby-lang.org/), 35 bytes
```
->n,s=0{s+=x=1+rand(n);x<n||redo;s}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y5Pp9jWoLpY27bC1lC7KDEvRSNP07rCJq@mpig1Jd@6uPZ/QWlJsUJatFnsfwA "Ruby – Try It Online")
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), ~~15~~ 14 bytes
```
{×r←?⍵:r⋄⍵+∇⍵}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qG9qZv6jtgkG/9OAZPXh6UVAyv5R71arokfdLUBa@1FHO5Cq/f8/DyhjmGqm8Kh3rkJeaW5SapFCfppCcWJuQU5qMVc1UFH0o95uIGUdWwvk7dLJO7z9Ue@KR52LQPof9exIO7Qi71HvFhOwCQVFmXklCimZxSVFmUmlJZn5eQA "APL (Dyalog Unicode) – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 77 76 bytes
```
import System.Random
f x=randomRIO(1,x)>>=(x!)
x!y|y<x=pure y|0<1=(y+)<$>f x
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/PzO3IL@oRCG4srgkNVcvKDEvJT@XK02hwrYIzAzy9Ncw1KnQtLOz1ahQ1OSqUKysqbSpsC0oLUpVqKwxsDG01ajU1rRRsQPq@Z@bmJlnm6agYWJl5ZlXoqkA1KVQUJSZV/IfAA "Haskell – Try It Online")
Thanks to killmous for one byte.
If `<|>` were in the prelude, we could do better with `MonadComprehensions`:
# [Haskell](https://www.haskell.org/), non-competing, 66 bytes
```
import System.Random
f x=do y<-randomRIO(1,x);[y|y<x]<|>(y+)<$>f x
```
[Try it online!](https://tio.run/##NcuxCsIwFEDR3a/I4JCgLQhONQ1Ipw4i1EUQh2BTG0zeC0mQBvrtxiK4Xs4dZXgpY7K2Dn0kDUL0aMqjc0Y/ZNRvRSjls2DsTy4pRGXLTkKPdjWQqe6RJF74X@jaM91tJ3a4pTnx6b6sNG0YX4tFZis11AOh@6pqITIiRE2c1xDz5zEY@Qy5uJ4QZN@gdV6NCoJGCF8 "Haskell – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 53 bytes
```
f=lambda n:random()*n//1or n+f(n)
from random import*
```
[Try it online!](https://tio.run/##RcoxDgIhFEXRnlX8EtA4jJlqElZijEFHlADvE4KFq0eJhe09t7zbk3Hs3dvk8nVzhLU6bJyl0pimmSth5yWU8JUz/YxCLlyb7rCLSPY0nPx3vVDAeB53ORtj1FmMGv910VCroFIDGsU9pcONX2gyKtE/ "Python 2 – Try It Online")
Uses the `or` short-circuiting idea from [ArBo's answer](https://codegolf.stackexchange.com/a/183091/20260). The expression `random()*n//1` generates a number from `0` to `n-1`, with `0` taking the place of a roll of `n`. The `or` takes the that number, except if it's zero (Falsey) it continues on to `n+f(n)`.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 13 bytes
```
ö)g@¶°X?X+ß:X
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=9ilnQLawWD9YK986WA&input=Mg)
Port of [Arnauld's answer](https://codegolf.stackexchange.com/a/183082/8340). Figured out how to make a recursive call ;)
Transpiled JS:
```
// U: Implicit input
// ö: generate a random number [0,U)
(U.ö())
  // g: run the result through a function
  .g(function(X, Y, Z) {
    // increment the result and compare to input
    return U === (++X)
      // if they are the same, roll again and add to current roll
      ? (X + rp())
      // if they are different, use current roll
      : X
   })
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~94~~ ~~81~~ 78 bytes
```
import System.Random
f n=do i<-randomRIO(1,n);last$((i+)<$>f n):[return i|i<n]
```
[Try it online!](https://tio.run/##HcqxCsIwFAXQvV9xhwwNWkHcYtq9U6GO4hBIig@Tl/IaB8F/j8X1cJ5ue4UYa6W0Zim4fbYS0ml27HNqFnDvM8h28od5nNrzkfU1uq2otqWDtmrYlzZ3CeUtDPqS5UdNjhg9fG4Age2w4AJjME4Yuey4CnGBgtQf "Haskell – Try It Online")
---
[Original](https://tio.run/##HcuxCsMgFAXQPV9xhwwJbQqlW2qyOwXSLxBU8qg@w9MO/XorXQ@cw@S3C6FWimeSgtc3Fxdvu2GbYufBi00gNckfdr0N9yuPT/INGeVwDHHlIwyCC9kNdBlVv7ZYoyHGAps6QKAmeDwwz9AbNJeGpxAX9JD6Aw "Haskell – Try It Online")
Quite similar to @dfeuer's, but using `do` notation.
* -13 bytes by removing whitespace, thanks to [@dfeuer](https://codegolf.stackexchange.com/users/38603/dfeuer)
* -3 bytes thanks to [this](https://codegolf.stackexchange.com/a/96371/86524)
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 49 bytes
```
for($a=$l="$args";$a-eq$l){$o+=$l=1..$a|Random}$o
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/Py2/SEMl0VYlx1ZJJbEovVjJWiVRN7VQJUezWiVfGyRuqKenklgTlJiXkp9bq5L///9/MwA "PowerShell – Try It Online")
Iterative method. Sets the input `$args` to `$a` and the `$l`ast roll (done so we enter the loop at least once). Then, so long as the last roll is `-eq`ual to the input, we keep rolling. Inside the loop we accumulate into `$o` the last roll, which is updated by creating a range from `1` to input `$a` and picking a `Random` element thereof. *(Honestly, I'm a little surprised that `$o+=$l=` works.)* Once we're out of the loop, we leave `$o` on the pipeline and output is implicit.
[Answer]
# [Forth (gforth)](http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/), 72 bytes
```
include random.fs
: f >r 0 begin i random 1+ >r i + r> i < until rdrop ;
```
[Try it online!](https://tio.run/##hY2xCsJAEER7v2KsLIJBwUollQfW2trE3F6ycO6FvU2hP38mYG/1hnkME5LasO3DglJYujh5grbi06sOGUcENIodntSzgH8K@2qpGRW0mXHGJMYR6jWNOJWruzncnbtgjQeME3yiLBsDCxu3kT8EGwiZyGOcR6TxvTrMbzU6/RvKFw "Forth (gforth) – Try It Online")
### Code Explanation
```
include random.fs      \ include library file for random
: f                    \ start a new word definition
  >r                   \ stick the input on the return stack (for easy access)
  0                    \ add a counter to hold the sum
  begin                \ start an indefinite loop
    i random 1+        \ generate a random number from 1 to n
    >r i + r>          \ add the result to the counter, use the return stack to save a few bytes
    i <                \ check if result was less than n
  until                \ end the loop if it was, otherwise go back to begin
  rdrop                \ remove n from the return stack
;                      \ end the word definition
```
[Answer]
## Batch, 70 bytes
```
@set t=0
:g
@set/at+=d=%random%%%%1+1
@if %d%==%1 goto g
@echo %t%
```
Takes input `n` as a command-line parameter `%1`. `d` is the current roll, `t` the cumulative total. Simply keeps rolling until `d` is not equal to `n`.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 bytes
```
x⁹X€Ä%ƇµḢ
```
[Try it online!](https://tio.run/##ASkA1v9qZWxsef//eOKBuVjigqzDhCXGh8K14bii/8Kzw4ckNDAww5DCof//Ng "Jelly – Try It Online")
A monadic link that takes n as its argument and returns a number generated by an exploding n-sided die. This generates 256 numbers from 1 to n and returns the first cumulative sum that is not a multiple of n. In theory this could return 256n, but even for a 2-sided die this would happen only one every \$2^{256}\$ times.
An alternative that doesn’t have this limitation is:
# [Jelly](https://github.com/DennisMitchell/jelly), 10 bytes
```
X³X¤+¥³ḍ¥¿
```
[Try it online!](https://tio.run/##y0rNyan8/z/i0OaIQ0u0Dy09tPnhjl4gtf//oc2H21VMDAwOTzi08P9/MwA "Jelly – Try It Online")
Note both TIO links generate 400 numbers to show the distribution. 
[Answer]
# [Python 3](https://docs.python.org/3/), ~~81~~ 72 bytes
```
from random import*
def f(x,a=0):
 while a%x<1:a+=randint(1,x)
 return a
```
[Try it online!](https://tio.run/##TYrLCoAgEADvfsVeArc6FD0OkR@zkJJQKouRfb3VIeg0wzDhiqt3Xc6G/Q5Mbnlg9@A5lmLRBoxMNakGJwHnajcNVKS5nahS72xdlG2dUADreLADyoHfaGSPKD4ffj4i5hs "Python 3 – Try It Online")
-9 bytes thanks to ArBo
## Explanation
```
import random             #load the random module              
def explodeDice(num):     #main function
    ans = 0                     #set answer to 0
    while a % num != 0:         #while a isn't a multiple of the input
        ans += random.randint(1, num) #add the next dice roll to answer
    return ans                  #return the answer
```
[Answer]
# TI-BASIC, ~~28~~ 23 bytes
*-5 bytes thanks to [this](https://codegolf.meta.stackexchange.com/questions/15025/what-are-the-standard-requirements-for-answering-a-random-challenge) meta post!*
```
Ans→N:0:Repeat fPart(Ans/N:Ans+randInt(1,N:End:Ans
```
Input is in `Ans`.  
Output is in `Ans` and is implicitly printed.
**Examples:**
```
4
              4
prgmCDGF11
              5
6
              6
prgmCDGF11
              3
```
**Explanation:**
```
Ans→N:0:Repeat fPart(Ans/N:Ans+randInt(1,N:End:Ans   ;full logic
Ans→N                                                ;store the input in "N"
      0                                              ;leave 0 in "Ans"
        Repeat fPart(Ans/N                 End       ;loop until the sum
                                                     ; is not a multiple of
                                                     ; the input
                               randInt(1,N           ;generate a random
                                                     ; integer in [1,N]
                           Ans+                      ;then add it to "Ans"
                                               Ans   ;leave the sum in "Ans"
                                                     ;implicitly print "Ans"
```
---
**Notes:**
* TI-BASIC is a tokenized language. Character count does ***not*** equal byte count.
[Answer]
# SmileBASIC 3, 49 bytes
Function `D N OUT R` implements exploding dice rolls recursively.
```
DEF D N OUT R
R=RND(N)+1IF R==N THEN R=R+D(N)
END
```
## Ungolfed
```
DEF D N OUT R  'N is sides and R is output param (shorter than using RETURN in this case)
 R=RND(N)+1  'random number in [1, N]
 IF R==N THEN R=R+D(N)  'if roll is same as N then roll again and add
END
```
Note that in SmileBASIC, functions can have multiple return values. If a function has one return value then `fun in OUT var` and `var = fun(in)` are exactly the same, which is why we can define the function in `OUT` form and also call it in an expression in the function body itself. If I had defined the function as `DEF D(N)` I would have to explicitly state `RETURN R` in the function body; mixing both syntaxes saved me bytes.
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 43 bytes (Iterative method)
```
param($n)do{$x+=1..$n|random}until($x%$n)$x
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/vyCxKDFXQyVPMyW/WqVC29ZQT08lr6YoMS8lP7e2NK8kM0dDpUIVKK9S8f//fzMA "PowerShell – Try It Online")
---
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 48 bytes (recursive method)
```
filter f{if($_-eq($x=1..$_|random)){$x+=$_|f}$x}
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/Py0zpyS1SCGtOjNNQyVeN7VQQ6XC1lBPTyW@pigxLyU/V1OzWqVC2xbIT6tVqaj9b1aT9v8/AA "PowerShell – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
X=Ð¿⁸S_
```
A monadic Link accepting an integer, `n`, which yields an integer.
**[Try it online!](https://tio.run/##y0rNyan8/z/C9vCEQ/sfNe4Ijv///78ZAA "Jelly – Try It Online")** Or see [the counts of \$10^5\$ runs](https://tio.run/##ASgA1/9qZWxsef//WD3DkMK/4oG4U1//4bqLyLc1w4figqzhuaLFknJH//80 "Jelly – Try It Online")
### How?
```
X=Ð¿⁸S_ - Link: integer, n
  Ð¿    - Collect up while...
 =  ⁸   - ...condition: equal to chain's left argument, n
X       - ...next value: random number in [1..n]
     S  - sum
      _ - subtract n (since the collection starts with [n])
```
[Answer]
# SmileBASIC, 41 bytes
```
INPUT N@L
S=S+RND(N)+1ON S MOD N GOTO@L?S
```
After reading:
> 
> Note that you should never output any multiples of n, since they will always explode.
> 
> 
> 
I realized that rather than checking if a dice roll was `n`, you can just repeat while the sum is a multiple of `n`.
[Answer]
# [AnyDice](https://anydice.com/), 36 bytes
Almost a built-in in the language:
```
function:f I:n{result: [explode dI]}
```
For this to be correct I have to abuse the infinite recursion depth assumption.
AnyDice limits the recursion depth with a global property maximum function depth.
the explode builtin however uses it's own; explode depth - which defaults to 2.
```
set "explode depth" to 99
```
Would add another 25 bytes; and would not really match the requirements since it's theoretically possible for a dice to explode more than 99 times.
The output of the function is a die, ie. an AnyDice built-in type that is a paring of results and probabilities for the result.
]