| Bytes | Lang | Time | Link |
|---|---|---|---|
| 051 | Befunge98 FBBI | 241107T210652Z | Valiant |
| 012 | Uiua | 241106T005450Z | noodle p |
| 006 | Japt x | 241105T103123Z | Shaggy |
| 061 | AWK | 241105T142841Z | xrs |
| 033 | C gcc | 230717T113249Z | landfill |
| 064 | Swift | 230718T192605Z | Bbrk24 |
| 091 | INTERCAL | 230718T160932Z | Saladin |
| 046 | PowerShell Core | 211027T202303Z | Julian |
| 030 | Raku | 230717T183222Z | bb94 |
| 007 | Thunno 2 | 230715T103006Z | The Thon |
| 007 | Jelly | 160610T172711Z | Dennis |
| 027 | Julia 1.0 | 211027T150511Z | MarcMush |
| 100 | λ2d | 220407T122928Z | jonatjan |
| 007 | Vyxal RṀ | 211027T083958Z | emanresu |
| 041 | Factor | 211027T182028Z | chunes |
| 056 | Java 8 | 190414T142421Z | Benjamin |
| 010 | Brachylog | 190414T054648Z | Unrelate |
| 060 | Kotlin 1.3+ | 180509T034918Z | snail_ |
| 063 | Python | 151214T012125Z | user4594 |
| 010 | APL Dyalog Unicode | 180509T211536Z | ngn |
| 063 | Python 2 | 180509T205836Z | mbomb007 |
| 012 | APL Dyalog Unicode | 151214T070133Z | Adá |
| 066 | F# | 180509T121926Z | Ciaran_M |
| 008 | 05AB1E | 180509T081648Z | Kevin Cr |
| 023 | Perl 5 p | 180509T044130Z | Xcali |
| 054 | SmileBASIC 3 | 180509T030535Z | snail_ |
| 052 | Tcl | 180509T014125Z | sergiol |
| 032 | Ruby | 160109T172148Z | daniero |
| 010 | Seriously | 151214T024930Z | user4594 |
| 038 | Mouse2002 | 151214T024511Z | cat |
| nan | BinaryEncoded Golfical | 151214T023646Z | SuperJed |
| 012 | MATL | 151214T022434Z | David |
| 021 | Microscript | 151214T014437Z | SuperJed |
| 051 | MATLAB | 151214T194641Z | costrom |
| 015 | TIBASIC | 151214T173019Z | lirtosia |
| 012 | MATL | 151214T105723Z | Luis Men |
| 011 | CJam | 151214T011949Z | Doorknob |
| 010 | Vitsy | 151214T104545Z | Addison |
| 037 | C | 151214T080831Z | Katenkyo |
| 013 | Seriously | 151214T013814Z | user4594 |
| 058 | Lisp | 151214T080532Z | sudo rm |
| 028 | R | 151214T011947Z | Alex A. |
| 033 | R | 151214T061635Z | mnel |
| 011 | Candy | 151214T061454Z | Dale Joh |
| 020 | Octave | 151214T055821Z | alephalp |
| 018 | TeaScript | 151214T031024Z | Downgoat |
| 011 | Pyth | 151214T024216Z | xnor |
| 008 | Pyth | 151214T022128Z | FryAmThe |
| 012 | Minkolang 0.14 | 151214T012240Z | El'e |
| 012 | Pyth | 151214T014034Z | Doorknob |
| 026 | Perl 6 | 151214T021907Z | Brad Gil |
| 032 | Mathematica | 151214T015549Z | LegionMa |
| 017 | APL | 151214T013716Z | Alex A. |
| 030 | Julia | 151214T012717Z | Alex A. |
| 038 | Javascript ES6 | 151214T012214Z | SuperJed |
Befunge-98 (FBBI), 51 bytes
Recursive!
&&#;+05j-f0+1?5j-f0+2?5j-f0+4?5j-80+8?>:6`3j@.$_5`;
Explanation
&&#;+05j-f0+1?5j-f0+2?5j-f0+4?5j-80+8?>:6`3j@.$_5`;
&& read two integers to the stack
#; if jumping, stop
+ add the values on the stack
0 push a 0 (the initial random value)
5j jump into the first bit randomiser
? randomise the IP direction (wrapping until east/west)
+1 50% chance we're going west; add 1 to the random value
j-f0 jump the IP 15 spaces backwards (east) to the next ?
5j 50% chance we're going east; jump to the next ?
j-f0+2?5j randomise the second bit (+2)
j-f0+4?5j randomise the third bit (+4)
j-80+8? randomise the fourth bit (+8)
align the IP east >
duplicate the top of the stack :
if top is >6, go west from _ 6`3j _
pop the unnecessary duplicate $
output the result and halt @.
push 1 if top >5, or 0 5`
start jumping and wrap ;
After the initial addition, this generates a random 4-bit number bitwise, using wrapping to force ? into a 50:50 east:west. The random number is then bucketed as follows:
7-F(9 combinations): Output the result6exactly (1 combination): Recurse with the result and 10-5(6 combinations): Recurse with the result and 0 (effectively rerolling the random number)
If j wrapped when jumping out of bounds a couple bytes could be shaved, but this doesn't appear to be in the specification, so negative jumps will have to do.
Uiua, 12 bytes
⍢(+1|<0.1⚂)+
Add the two numbers, then repeatedly add one while getting a random float gives less than 0.1.
Try it: Uiua pad, with trial results included:
Results for 5000 trials of 4 + 5:
9: 4530 (90.6%)
10: 417 (8.34%)
11: 48 (0.96%)
12: 4 (0.08%)
13: 1 (0.02%)
Japt -x, 8 6 bytes
Takes input as an array of 2 integers.
p@Aö}a
p@Aö}a :Implicit input of array
p :Push
@ : Function
A : 10
ö : Random integer in range [0,A)
} : End function
a : Run until truthy (not 0) and return 0-based iteration index
:Implicit output of sum of resulting array
AWK, 61 bytes
func f(x,y){print 9<rand()*11?f(x,y+1):x+y}{srand();f($1,$2)}
You can technically do it without srand(), but you'll basically always get the same result when you repeat input.
C (gcc), 33 bytes
f(x,y){x=rand()%10?x+y:1+f(x,y);}
Alternatively:
f(x,y){x=(rand()%10?x:f(x,1))+y;}
Or a 3rd way to arrange it:
f(x,y){x=rand()%10?x+y:f(x,y+1);}
Swift, 64 bytes
func f(a:Int,b:Int)->Int{.random(in:0...9)==0 ?f(a:a,b:b+1):a+b}
SwiftFiddle link to try it yourself. The code should be fairly straightforward.
INTERCAL, 91 bytes
DOWRITEIN.1+.2PLEASE(1000)NEXTDO.1<-.3DO%10COMEFROM(1)(1)DON'T(1020)NEXTONCEPLEASEREADOUT.1
Explanation:
DO WRITE IN .1 + .2 // Get input numbers
PLEASE (1000) NEXT // Add them together
DO .1 <- .3 // Copy the result into .1
DO %10 COME FROM (1) // Loop back 10% of the time
(1) DON'T (1020) NEXT ONCE // Increment .1 every loop iteration except the first
PLEASE READ OUT .1 // Output result
// Fallthrough to stdlib syntax error/exit
Thunno 2, 7 bytes
+(Tɼḅ;⁺
Explanation
+(Tɼḅ;⁺ # implicit input
+ # add the two inputs together
( # (while)
Tɼ # random item from [1..10]
ḅ # is equal to 1
; # (do)
⁺ # increment
# implicit output
Jelly, 7 bytes
‘⁵XỊ¤¿+
How it works
‘⁵XỊ¤¿+ Main link. Arguments: n, m (integers)
¤ Combine the three atoms to the left into a niladic chain.
⁵ Yield 10.
X Pseudo-randomly generate a positive integer not greater than 10.
Ị Insignificant; test if the generated integer is 1 (or less).
¿ While chain yields 1:
‘ Increment n.
+ Add m to the result.
Julia 1.0, 27 bytes
a\b=rand()<.1 ? 1+a\b : a+b
Bonus: for 2 additionnal bytes, we can overwrite the + function for even more fun:
a+b=rand()<.1 ? -~a+b : a- -b
λ-2d, 100 squares
Found this language on Hackernews yesterday and I wanted to try it at a small program
You can test it by going to this page and going to "file" -> "load JSON" with a file containing this content
{"22,6":"func_def","24,6":"func_def","23,6":"end_s","25,6":"end_s","26,6":"end_s","24,8":"func_call","24,11":"func_call","25,7":"wire_nw","24,7":"wire_se","25,8":"wire_sw","25,9":"wire_ns","24,10":"wire_sw","25,10":"func_call","23,9":"wire_ns","23,8":"wire_ns","23,7":"wire_ns","23,10":"wire_ne","25,11":"wire_nw","26,9":"wire_ns","26,8":"wire_ns","26,7":"wire_ns","26,10":"wire_nw","24,12":"op_plus","22,5":"wire_se","23,5":"label","24,5":[0,0,1,0,0,0,1,0,1,0,0,1,1,1,0,1,0,0,0,1,1,0,0,0,1],"32,8":"func_call","33,9":"func_call","33,8":"wire_sw","34,9":"wire_nw","34,8":"func_call","34,7":"func_def","35,7":"end_s","36,7":"end_e","35,8":"wire_sw","36,9":"func_call","35,9":"wire_ns","35,10":"wire_ne","36,10":"wire_nw","36,8":"func_def","37,8":"end_s","37,7":"wire_nw","37,6":"wire_ns","37,5":"joint_nse","38,6":"func_call","38,7":"op_plus","38,5":"wire_sw","39,6":"wire_sw","39,7":"wire_ne","40,7":"wire_nw","40,6":"func_call","40,5":"num_1","41,6":"wire_sw","38,8":"end_e","37,9":"wire_we","42,4":"wire_sw","39,4":"wire_we","40,4":"wire_we","41,4":"wire_we","36,4":"func_def","37,4":"end_s","38,4":"end_e","36,3":"wire_se","37,3":"label","38,3":[0,0,1,1,0,0,0,0,0,1,0,1,0,1,0,1,1,1,0,0,0,1,0,1,0],"24,9":[0,0,1,1,0,0,0,0,0,1,0,1,0,1,0,1,1,1,0,0,0,1,0,1,0],"33,16":"entry","34,16":"end_s","35,16":"end_e","36,16":"frame_tl","47,16":"wire_sw","47,18":"wire_nw","36,18":"wire_ne","36,17":"wire_ns","47,17":"wire_ns","37,16":"wire_we","38,16":"wire_we","39,16":"wire_we","40,16":"wire_we","41,16":"wire_we","42,16":"wire_we","43,16":"wire_we","44,16":"wire_we","45,16":"wire_we","46,16":"wire_we","46,18":"wire_we","45,18":"wire_we","44,18":"wire_we","43,18":"wire_we","41,18":"wire_we","40,18":"wire_we","38,18":"wire_we","37,18":"wire_we","39,18":"wire_we","42,18":"wire_we","35,20":"func_call","34,17":"wire_ns","34,18":"wire_ns","34,19":"wire_ns","34,20":"wire_ns","36,20":"wire_sw","36,21":"wire_nw","35,21":"wire_nswe","34,21":"wire_ne","35,22":"wire_nw","34,22":"wire_se","34,23":"wire_ns","34,24":"wire_ns","34,25":"wire_ne","35,25":"wire_nswe","35,24":"func_call","36,25":"wire_nw","36,24":"wire_sw","35,26":[0,0,1,0,0,0,1,0,1,0,0,1,1,1,0,1,0,0,0,1,1,0,0,0,1],"35,19":"num_1","35,23":"num_1","19,3":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"30,7":"func_call","31,9":"func_call","33,10":"op_if","30,8":"op_rand","30,6":"num_1","31,6":"num_1","32,7":"num_1","31,7":"wire_sw","31,8":"wire_ns","32,9":"wire_nw","31,10":"op_gt","41,7":"func_call","41,8":[0,0,1,1,0,0,0,0,0,1,0,1,0,1,0,1,1,1,0,0,0,1,0,1,0],"42,7":"wire_sw","42,8":"wire_ns","42,9":"wire_nw","41,9":"wire_we","39,8":"wire_we","40,8":"wire_sw","40,9":"wire_ne","39,9":"wire_sw","43,6":"wire_sw","42,6":"wire_ne","39,10":"wire_ne","43,10":"wire_nw","42,5":"wire_ns","43,7":"wire_ns","43,8":"wire_ns","43,9":"wire_ns","38,9":"wire_we","40,10":"wire_we","41,10":"wire_we","42,10":"wire_we"}
The screenshot and square count only show the function, the full program also have a call to the function using this structure

where you can replace both 42 by the input you want
pseudo code using a jslike syntax
alexAdd = a => b => a + maybeInc(b)
maybeInc = c => random(11) > 1 ? c : maybeInc(c + 1)
You can get knowledge of how to read the program by reading the cheatsheet sample in the editor and the release blog post
Vyxal RṀ, 7 bytes
+{₀℅¬|›
Vyxal, 8 bytes
+{₀ʁ℅¬|›
+ # Add the two (implicitly input) numbers
{ # While...
℅ # Random choice of...
ʁ # range 0 to... (Unnecessary in flagged version)
₀ # 10 (exclusive)
¬ # Is 0
|› # Increment
Factor, 41 bytes
: a ( x y -- z ) .1 [ 1 + a ] [ + ] ifp ;
ifp is a version of if that takes a probability instead of a boolean. Otherwise, it's just a simple recursive definition (which is verbose in Factor because of the mandatory stack effect declaration).
Java 8, 57 56 bytes
4 years and no Java answer? Shame.
int a(int a,int b){return.1>Math.random()?a(a,b+1):a+b;}
Try it online!
-1 byte thanks to ceilingcat
Brachylog, 10 bytes
∧9ṙ9&↰+₁|+
Takes input as a list of numbers. The TIO header runs the predicate on the input infinitely and prints the results.
ṙ A random integer from zero to
9 nine
∧ which is not necessarily the input
9 is nine,
& and the input
↰ passed through this predicate again
+₁ plus one
| is the output, or, the input
+ summed
is the output.
Kotlin (1.3+), 60 bytes
fun a(b:Int,c:Int):Int=if((0..9).random()<1)a(b,c+1)else b+c
A solution that uses the new cross-platform random features added in Kotlin 1.3.
Kotlin (JVM), 59 bytes
fun a(b:Int,c:Int):Int=if(Math.random()<.1)a(b,c+1)else b+c
Works on the JVM version because java.lang.Math is automatically imported.
Kotlin (<1.3), 65 bytes
This version is "cross-platform" Kotlin since it doesn't depend on any Java features.
fun a(b:Int,c:Int):Int=if((0..9).shuffled()[0]<1)a(b,c+1)else b+c
The "randomness" is obtained by shuffling the inclusive range 0..9, which generates a List<Int>, and then checking the first element of that list. Assuming shuffled() is perfectly random (I have no idea how random it actually is) there is a 10% chance of the first element being 0.
Python, 66 65 64 63 bytes
from random import*
g=lambda*s:randint(0,9)and sum(s)or g(1,*s)
Thanks to Sherlock9 for the corrections and the byte saved.
Thanks to Mathias Ettinger for another byte.
Thanks to mbomb007 for a byte.
Python 2, 63 bytes
Returns the result as a string.
from random import*
f=lambda a,b:random()>.1and`a+b`or f(a,b+1)
Test program showing probability distribution: Try it online!
APL (Dyalog Unicode), 13 12 bytesSBCS
Basically the same as FryAmTheEggman's Pyth solution. -1 thanks to Erik the Outgolfer.
Anonymous tacit infix function.
{⍵+1=?10}⍣=+
+ add the arguments
{…}⍣= apply the following function until two successive applications have the same result:
?10 random integer in the range 1–10
1= is one equal to that? (i.e. 1⁄10th chance)
⍵+ add the argument to that
F#, 66 bytes
let rec a x y=(if(System.Random()).Next(10)=9 then a x 1 else x)+y
The if statement is like a function itself. If the random number is 9 (in the range [0, 10)) then perform Alex-addition on x and 1 and return that value. Otherwise return just x.
Then add the result of the if statement to y, and return it.
05AB1E, 15 14 13 8 bytes
+[TLΩ≠#>
-5 bytes thanks to @Emigna by placing the > (increment by 1) after the # (break loop).
Explanation:
# Implicit inputs `a` and `b`
+ # Sum of these two inputs
[ # Start an infinite loop
Ω # Random integer
TL # in the range [1, 10]
≠ # If this integer isn't exactly 1:
# # Stop the loop
> # Increase the result by 1
# Implicitly output the result
SmileBASIC 3, 54 bytes
Recursive function that takes two numbers.
DEF A(B,C)IF RND(10)THEN RETURN B+C
RETURN A(B,C+1)END
Ruby, 32 bytes
Can't believe there was no Ruby answer. Here's a pretty basic lambda function:
f=->a,b{rand(10)<1?f[a,b+1]:a+b}
But why not do it properly? Here's some ungolfed meta-Alexification:
module Alex
def +(other)
other += 1 if rand(10) == 7
super
end
end
[Fixnum, Bignum, Float].each { |klass| klass.prepend(Alex) }
# testing
p 1000.times.count { 1 + 1 == 3 } #=> 87
p 1000.times.count { 1 + 1 == 4 } #=> 19
p 1000.times.count { 1 + 1.3 == 5.3 } #=> 1
Seriously, 10 bytes
,Σ1±╤_G_\+
This program generates a random variable from a geometric distribution by transforming a uniform distribution. It takes input as a list: [2,3] (braces optional). Try it online.
Explanation:
,Σ1±╤_G_\+
,Σ get sum of input
1±╤_ push ln(0.1)
G_ push ln(random(0,1))
\ floored division
+ add
Given a random variable X ~ Uniform(0, 1), it can be transformed to a random variable Y ~ Geometric(p) with the formula Y = floor(log(X)/log(p)).
Mouse-2002, 41 39 38 bytes
No recursion.
&TIME &SEED ??&RAND k*&INT 9=[+1+!|+!]
Explained:
&TIME &SEED ~ painfully obvious
? ? ~ get some input
&RAND 10 * &INT 8 > ~ get a random number 0-1 * 10, make it an int & check if >8
[ ~ if true
+ 1 + ! ~ add the input then add 1 and print
| ~ else
+ ! ~ add and print
] ~ endif
$ ~ (implicit) end of program
Or, if you're a functional programming fanboy, and recursion is your deal then 57 bytes:
&TIME &SEED #A,?,?;!$A&RAND k*&INT 9=[#A,1%,2%1+;|1%2%+]@
Explained:
&TIME &SEED ~ painfully obvious
#A, ?, ?; ! ~ call with input & print
$A ~ begin a definition of a function A
&RAND 10 * &INT 8 > ~ same as above
[
#A, 1%, 2% 1 +; ~ call with args and add 1
|
1% 2% + ~ else just add
]
@ ~ end func
$ ~ as above
Binary-Encoded Golfical, 32 29+1 (-x flag) = 30 bytes
Hexdump (manually edited to correct for a bug in the image-to-binary part of the transpiler, which has since been fixed):
00 B0 02 15 14 0C 01 14 15 14 1B 1E 3A 14 0C 01
14 00 0A 14 38 00 01 23 1D 4C 14 17 14
This encoding can be converted back into the original graphical representation using the included Encoder utility, or run directly using the -x flag.
Original image:

Magnified 50x:

Explanation: The top row is the main block. It reads a number, copies it to the right, reads another number, adds them, copies the result to the right, does some RNG stuff, and, with probability 90%, prints the result of the addition. The rest of the time, it is sent to the bottom row, where it puts a one in the first cell and goes back to the main row just before the addition instruction (using a north turn then an east turn).
MATL, 14 13 12 bytes
is`r.1<tb+w]
This is just the loop method, add the inputs (entered as [a b]) then keep adding one while a uniform random number between 0 and 1 is less than 0.1. Full description below:
i % input [a b]
s % sum a and b
` % do...while loop
r % get a uniformly distributed pseudorandom numbers between 0 and 1
.1 % push 0.1 onto the stack
< % is the random number less than 0.1?
t % duplicate the T/F values
b % bubble a+b to the top of the stack
+ % add the T/F to a+b
w % swap elements in stack to get the other T/F back to exit/continue the loop
] % end
Took off 1 byte by changing input spec (from ii+ to is).
The old way was based on taking the base-10 log of a random number between 0 and 1 to work out the amount to add to a+b, however it would only work up to 15 repetitions due to floating point accuracy.
iir10,2$YlZo-+
In this code, 10,2$YlZo- does base-10 logarithm of the random number and rounds up to the nearest integer.
Microscript, 29 21 bytes
isi+vzr10!{l1vzr10!}l
I tried to make a Microscript II answer but for some reason I couldn't get the addition loop to work right :(
MATLAB , 51 bytes
function f(a,b)
if rand > .1;a+b;else;f(a,b+1);end
Result is found in the 'ans' automatic variable
TI-BASIC, 15 bytes
While rand<.1
Ans+.5
End
sum(Ans
This takes the input as a two-element list from Ans. While a random number is less than 0.1, it does vectorized addition to 0.5 on the list. Increasing each element by 0.5 increases the sum by 1. I believe this is the shortest TI-BASIC solution.
The 9-byte program sum(Ans)-int(log(10rand doesn't work, because rand only has 14 digits of precision, and thus it can't give a number less than 10-14.
MATL, 12 13 14 bytes
r.1H$YlY[ihs
Input is of the form [3 4], that is, a row vector with the two numbers.
Example
>> matl r.1H$YlY[ihs
> [3 4]
7
Explanation
This generates the geometric random variable without loops, by directly applying a a transformation to a uniform random variable. Note that log0.1 a is used instead of log a / log 0.1 to save 1 byte.
r % random number with uniform distribution in (0,1)
.1 % number 0.1
H$ % specify two inputs for next function
Yl % logarithm in specified base (0.1)
Y[ % floor. This produces the geometric random variable with parameter 0.1
i % input vector of two numbers
h % concatenate horizontally with the previously generated random value
s % sum of vector elements
CJam, 12 11 bytes
{{+Amr!}h;}
Thanks to @MartinBütter for saving a byte with this super clever trick!
{ }
{ }h Do-while that leaves the condition on the stack.
+ Add: this will add the numbers on the first iteration...
Amr! ... but a `1` (i.e. increment) on future ones.
; Pop the remaining 0.
Old answer:
{+({)Amr!}g}
Explanation:
{ } A "function."
+ Add the input numbers.
( Decrement.
{ }g A while loop.
) Increment.
Amr Random number [0,9).
! Boolean NOT.
The basic algorithm is "while (0.1 chance), increment the number," which eliminates the need for the recursion.
Vitsy, 12 10 bytes
aR)[1+0m]+
aR Get a random number in [0,10)
)[ ] If its truncated int is 0, do the stuff in brackets.
1+0m Add 1 to one of the items and execute the 0th index of code.
+ Add the numbers together.
Note that this has a tiny chance of a stack overflow error. We're talking (.1)^400 chance. It also exits on error due to how I caused recursion.
C, 71 51 39 37 bytes
First code-golf, done in C... I don't think it will beat anything, and may be golfed down a lot
EDIT 3: cuted 2 bytes thanks to @Mego , by writing .1 instead of 0.1 and rewriting the ternary operator
a(x,y){return(rand()<.1?a(1,y):y)+x;}
EDIT 2: cuted 12 bytes, following gnu99, every variable is an int if not stated otherwise. Same goes for the return type of function
a(x,y){return rand()<0.1?a(x,y+1):x+y;}
EDIT : cuted 20 bytes, forgot that basic .h aren't necessary in C99 (using gcc for instance). It will produce a warning :)
int a(int x,int y){return rand()<0.1?a(x,y+1):x+y;}
71 Bytes solution :
#include <stdlib.h>
int a(int x,int y){return rand()<0.1?a(x,y+1):x+y;}
If you want to see lots of output, you can use the following code
#include <stdio.h>
#include <stdlib.h>
int a(int x,int y){return rand()<0.1?a(x,y+1):x+y;}
int main(void)
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
printf("%d + %d = %d\n",i,j,a(i,j));
return 0;
}
Seriously, 13 bytes
,,1W+9uJYWDkΣ
Uses a similar strategy to Doorknob's CJam answer (increment number while random float is less than 0.1), except it uses integers, and increments while random integer in [0,9] is less than 1. The lack of easy recursion hurts.
Try it online (needs manual input)
Explanation:
,,1W+9uJYWDkΣ
,,1 get input, push 1
W W while loop:
+ add top two elements
9uJ push a random integer in [0, 9]
Y push 1 if random value is falsey (0) else 0
DkΣ decrement top value and add everything together
The while loop leaves the stack like this:
n: the # of times the random value was < 0.1, plus 1
b: the second input
a: the first input
Shifting n up by 1 is necessary to get the while loop to run, since 0 is falsey. It's easily dealt with by decrementing n after the while loop, so the final result is a + b + (n - 1).
Lisp, 58 bytes
My first time writing Lisp!
(defun +(a b)(if(<(random 10)9)(- a(- b))(- a(-(+ b 1)))))
You can use this special addition exactly as you would usually add in Lisp:
> (+ 1 3)
4
> (+ 1 3)
5
I would love to hear suggestions as I am brand new to the language.
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)
}
R 33 bytes
f=function(a,b)a+b+rbinom(b,b,.1)
Edit: now simulates recursive application of +1 for each 1 in b.
Candy, 11 bytes
+#10H{.}10g
The long form is:
add # add two numbers on the stack
number digit1 digit0 rand # random int from 0 to 9
if # if non-zero
retSub # terminate loop
endif
digit1 # push 1 to stack
digit0 goto # loop to start
Octave, 20 bytes
@(a,b)a+b+geornd(.9)
Sum of the inputs, plus a random sample from the geometric distribution with parameter 0.9.
TeaScript, 18 bytes 21
#$r<.1?f(l,i¬):l+i
This is a TeaScript function. Assign it to a variable or just run it directly.
Pyth, 11 bytes
+sQ-18l`hO0
A direct Pyth port of my Python answer.
+ Add up
sQ the sum of the input and
- the difference of
18 18 and
l` the string length of
hO0 one plus a random number in [0,1)
Pyth, 8
u+G!OTsQ
This uses Pyth's second mode on reduce, that looks for repeated input then exits.
Explanation
u+G!OTsQ ## Implicit: Q=eval(input())
u sQ ## reduce with 2 arguments, which causes a loop until the reduce gets the
## same argument twice
+G ## lambda G,H: G + ...
!OT ## boolean not of random value from 0 to 9 inclusive
If the extra alex-add occurs it will run again, but if not then it exits.
Minkolang 0.14, 19 11 12 bytes
This is the "function" version; it assumes a and b are already on the stack, pops them off and pushes the modified version of a+b. The closest equivalent to functions in Minkolang is to use F, which pops off b,a and jumps to (a,b) in the codebox. Then when the program counter hits an f, it jumps back to where F was used.
(+$01$h`d)xf
This is the full program version, 15 bytes. (nn takes two numbers from input and N. outputs the result and stops.)
nn(+$01$h`d)xN.
I stole the algorithm from Doorknob's answer; the while loop repeats so long as the generated random number is less than 0.1, adding 1 each time. Try it here (full program version) and run it 100 times here.
Explanation
( Open a while loop
+ Adds the top two items of the stack
$0 Pushes 0.1
1$h Pushes a random number between 0.0 and 1.0, inclusive
` Pops b,a and pushes a > b
d Duplicate the top of stack
) Close the while loop when the top of stack is 0
x Dump the extra, leading 0
The cleverest part here is d. The top of stack at that point in time will be either 0 or 1. If it's 0, the while loop exits. Otherwise, it continues. As I duplicate the top of stack, it will be [a+b,1] the second time through the loop, so the + at the beginning adds the 1 (and likewise for subsequent trips).
Pyth, 14 12 bytes
KsQW!OT=hK;K
My first real Pyth golf!
Takes input on STDIN in the format a,b.
Explanation:
KsQ read STDIN, assign sum to variable K
W while...
!OT not rand(10)
=hK; increment K
K implicit-output of K
Thanks to @FryAmTheEggman for shaving off two chars by giving me a shorter way to increment a variable!
Perl 6, 26 bytes
Actually doing it recursively:
sub f{.1>rand??f |@_,1!![+] @_} # 31 bytes
Create a possibly empty sequence of 1s followed by the arguments, then sum them all together.
{[+] {1}...^{rand>.1},|@_} # 26 bytes
( it can actually take any number of arguments )
usage:
# give the lambda a name
my &f = {...}
say f 1,2; # one of 3,4,5 ... *
Mathematica, 32 bytes
If[RandomReal[]<.1,+##,#0@##+1]&
Explanation:
& A function returning
If[ ] if
RandomReal[] a random number in [0,1)
< is less than
.1 .1
, , then
+ the sum of
## all arguments
, , otherwise,
#0@ this function applied to
## all arguments
+ plus
1 one.
Note that this function works for any number of inputs.
APL, 17 bytes
{1=?10:⍺∇⍵+1⋄⍺+⍵}
This is an unnamed dyadic function.
Ungolfed:
{1=?10: ⍝ If a random number between 1 and 10 is 1,
⍺∇⍵+1 ⍝ Recurse on the inputs with one incremented
⋄⍺+⍵} ⍝ Otherwise return the sum of the inputs
Julia, 30 bytes
f(a,b)=rand()>0.9?f(a,b+1):a+b
This is a recursive function f that accepts two numbers and returns a number of the same type. It should work fine for any numeric type.
First we check whether a random float between 0 and 1 is greater than 0.9. If so, we recurse with a little extra somethin' somethin', otherwise we just add.
Javascript ES6, 38 bytes
f=(a,b)=>Math.random()<.1?f(a,b+1):a+b
