Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

-2 bytes with scan()
Source Link
Robin Ryder
  • 15.8k
  • 2
  • 25
  • 71

R, 4139 bytes

functionn=scan(n)n*rgeom;n*rgeom(1,1-1/n)+sample(n-1,1)

Try it online! 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, 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.

R, 41 bytes

function(n)n*rgeom(1,1-1/n)+sample(n-1,1)

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, 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.

R, 39 bytes

n=scan();n*rgeom(1,1-1/n)+sample(n-1,1)

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, 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.

longer explanation
Source Link
Robin Ryder
  • 15.8k
  • 2
  • 25
  • 71

R, 41 bytes

function(n)n*rgeom(1,1-1/n)+sample(n-1,1)

Try it online!

Explanation: for a die withthis 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 followsis the number of successes before the first failure. This corresponds to a \$\mathrm{Geometric}\left(1-\frac{1}{n}\right)\$ distribution (number of successes beforesee the first failure, where probability of success is \1ドル-\frac1n\$)wikipedia page, which each bringdefines 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.

R, 41 bytes

function(n)n*rgeom(1,1-1/n)+sample(n-1,1)

Try it online!

Explanation: for a die with \$n\$ sides, the number of explosions follows a \$\mathrm{Geometric}\left(1-\frac{1}{n}\right)\$ distribution (number of successes before the first failure, where probability of success is \1ドル-\frac1n\$), which each bring \$n\$ to the total. The final roll follows a \$\mathrm{Uniform}(1,2,\ldots,n-1)\$ distribution which we add to the total.

R, 41 bytes

function(n)n*rgeom(1,1-1/n)+sample(n-1,1)

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, 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.

R, 41 bytes

function(n)n*rgeom(1,1-1/n)+sample(n-1,1)

Try it online!

Explanation: for a die with \$n\$ sides, the number of explosions follows a \$Geometric\left(1-\frac{1}{n}\right)\$\$\mathrm{Geometric}\left(1-\frac{1}{n}\right)\$ distribution (number of successes before the first failure, where probability of success is \1ドル-\frac1n\$), which each bring \$n\$ to the total. The final roll follows a \$Uniform(1,2,\ldots,n-1)\$\$\mathrm{Uniform}(1,2,\ldots,n-1)\$ distribution which we add to the total.

R, 41 bytes

function(n)n*rgeom(1,1-1/n)+sample(n-1,1)

Try it online!

Explanation: for a die with \$n\$ sides, the number of explosions follows a \$Geometric\left(1-\frac{1}{n}\right)\$ distribution (number of successes before the first failure, where probability of success is \1ドル-\frac1n\$), which each bring \$n\$ to the total. The final roll follows a \$Uniform(1,2,\ldots,n-1)\$ distribution which we add to the total.

R, 41 bytes

function(n)n*rgeom(1,1-1/n)+sample(n-1,1)

Try it online!

Explanation: for a die with \$n\$ sides, the number of explosions follows a \$\mathrm{Geometric}\left(1-\frac{1}{n}\right)\$ distribution (number of successes before the first failure, where probability of success is \1ドル-\frac1n\$), which each bring \$n\$ to the total. The final roll follows a \$\mathrm{Uniform}(1,2,\ldots,n-1)\$ distribution which we add to the total.

Source Link
Robin Ryder
  • 15.8k
  • 2
  • 25
  • 71
Loading

AltStyle によって変換されたページ (->オリジナル) /