Skip to main content
added 1291 characters in body
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119

R, 60 4747 28 bytes

f=functionfunction(a,b)if(runifa+b+rgeom(1)>0,.9)f(a,b+1)else a+b

This is an unnamed function object that accepts two numbers and returns a number. It does not use recursion.

As xnor pointed out in a comment, this problem can be viewed as simply adding two numbers plus a geometric random variable with failure probability 1/10.

Why is that true? Think about it in terms of recursion, as it's described in the post. In each iteration we have a 10% chance of adding 1 and recursing, and a 90% chance of exiting the function without further addition. Each iteration is its own independent Bernoulli trial with outcomes "add 1, recurse" (failure) and "exit" (success). Thus the probability of failure is 1/10 and the probability of success is 9/10.

When dealing with a series of independent Bernoulli trials, the number of trials needed to obtain a single success follows a geometric distribution. In our case, each recursion means adding 1, so when we do finally exit the function, we've essentially counted the number of failures that occurred before the first success. That means that the amount that the result will be off by is a random variate from a geometric distribution.

Here we can take advantage of R's expansive suite of probability distribution built-ins and use rgeom, which returns a random value from a geometric distribution.

Ungolfed:

f <- function(a, b) {
    # Check a random uniform float against 0.9
+ b + rgeom(n if= (runif(1), >prob = 0.9)
        f(a, b + 1)
    else
        a + b
}

R, 60 47 bytes

f=function(a,b)if(runif(1)>0.9)f(a,b+1)else a+b

Ungolfed:

f <- function(a, b) {
    # Check a random uniform float against 0.9
    if (runif(1) > 0.9)
        f(a, b + 1)
    else
        a + b
}

R, 60 47 28 bytes

function(a,b)a+b+rgeom(1,.9)

This is an unnamed function object that accepts two numbers and returns a number. It does not use recursion.

As xnor pointed out in a comment, this problem can be viewed as simply adding two numbers plus a geometric random variable with failure probability 1/10.

Why is that true? Think about it in terms of recursion, as it's described in the post. In each iteration we have a 10% chance of adding 1 and recursing, and a 90% chance of exiting the function without further addition. Each iteration is its own independent Bernoulli trial with outcomes "add 1, recurse" (failure) and "exit" (success). Thus the probability of failure is 1/10 and the probability of success is 9/10.

When dealing with a series of independent Bernoulli trials, the number of trials needed to obtain a single success follows a geometric distribution. In our case, each recursion means adding 1, so when we do finally exit the function, we've essentially counted the number of failures that occurred before the first success. That means that the amount that the result will be off by is a random variate from a geometric distribution.

Here we can take advantage of R's expansive suite of probability distribution built-ins and use rgeom, which returns a random value from a geometric distribution.

Ungolfed:

f <- function(a, b) {
    a + b + rgeom(n = 1, prob = 0.9)
}
added 22 characters in body
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119

R, 6060 47 bytes

f=function(a,b)if(samplerunif(0:1,1,,c(0)>0.9,0.1)))f(a,b+1)else a+b

Ungolfed:

f <- function(a, b) {
    if# (sample(0:1,Check sizea =random 1,uniform probfloat =against c(0.9, 
 0.   if (runif(1)) > 0.9)
        f(a, b + 1)
    else
        a + b
}

R, 60 bytes

f=function(a,b)if(sample(0:1,1,,c(0.9,0.1)))f(a,b+1)else a+b

Ungolfed:

f <- function(a, b) {
    if (sample(0:1, size = 1, prob = c(0.9, 0.1)))
        f(a, b + 1)
    else
        a + b
}

R, 60 47 bytes

f=function(a,b)if(runif(1)>0.9)f(a,b+1)else a+b

Ungolfed:

f <- function(a, b) {
    # Check a random uniform float against 0.9 
    if (runif(1) > 0.9)
        f(a, b + 1)
    else
        a + b
}
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119

R, 60 bytes

f=function(a,b)if(sample(0:1,1,,c(0.9,0.1)))f(a,b+1)else a+b

Ungolfed:

f <- function(a, b) {
    if (sample(0:1, size = 1, prob = c(0.9, 0.1)))
        f(a, b + 1)
    else
        a + b
}