g | x | w | all
Bytes Lang Time Link
003Uiua240724T073438ZEurope20
026Setanta240724T035212Zbb94
004JCram240723T134944ZKamila S
005APL220713T165112ZVadim Tu
007Befunge93210501T204509Zuser1011
029Thue210501T093339ZConor O&
005Random Brainfuck210503T195122ZHelena
009R210501T101102ZDominic
003><>210502T205125ZSE - sto
009R210501T095839ZRobin Ry
006Jelly210501T054457Zhyper-ne
031Python 3210501T134905ZTrebor
003MATL210501T105545ZLuis Men
007Charcoal210501T082509ZNeil
024Bash210501T075735ZNoodle9
005Retina210501T074521ZNeil
003Vyxal210501T063243ZAaroneou
004Pyth210501T072058ZManish K
118C++ gcc210501T054445ZHuỳnh Tr
037Python 3210501T050930Zhyper-ne

Uiua, 3 characters

÷⚂1

Try it online! Divides 1 by a random number from 0 to 1. Pretty simple, huh?

Setanta, 26 bytes

scriobh(1/(1-rand@mata()))

Try it on try-setanta.ie

JCram, 4 bytes (SBCS)

⋆X⍕↑

In ES6, it's the obvious:

a=>1/Math.random()

APL, 5 bytes

÷1-?0

The program generates a random number from the range [0; 1), then substracts it from 1 to get the range (0; 1], and takes the reciprocal, so we get a number from the range [1; +∞).

Befunge-93, 8 7 bytes

2?.#*@#

Try it online!

This is basically just the St. Petersburg paradox. Every time the pointer reaches the ? and goes either left or right, it has a 1/2 chance of doubling the top of the stack and a 1/2 chance of just outputting the top of the stack and halting.

Explanation of the code:

2?.#*@#

2        Push 2 onto the stack.
 ?       Change the direction of the pointer to a random direction:
2   *      If left, double the top of the stack.
  .  @     If right, output the top of the stack and halt.
 ?         If up/down, the pointer returns to the ?.

Thue, 29 bytes

Edit: +9 bytes to fix issue spotted by @RossMillikan.

1::=.10
10::=~1
.0::=~0
::=
1

Try it online! The TIO interpreter requires a trailing newline, and outputs each digit on a different line, which is not required. Here is an interpreter without these restrictions.

Outputs in binary (meta link). Each step has a 50% chance to double the binary integer, or to start outputting it. The proven formula from other answers.

Random Brainfuck, 5 bytes

+[?.]

Throw a D256 until you throw a 0

Try it online!

R, 9 bytes

1/rexp(1)

Try it online!

rexp(1) - a single random sample from an exponential distribution with a (default) rate parameter of 1 - is the shortest R random function call with a nonzero probability density at zero (there are other shorter random functions [rt and rf], but unfortunately they each lack default values for required parameters, so the function calls are no shorter*).

*Edit: no shorter, but both can be made just-as-short: see Robin Ryder's answer

><>, 3 bytes

1nx

Try it online!

Has an infinite expected value, due to diverging more quickly than exponentially more unlikely repunits. Will nevertheless always terminate when it empties its own stack, when the random trampoline takes more left turns than right turns.

R, 9 bytes

rf(1,1,1)

Try it online!

Outputs a realization of the \$F(1,1)\$ distribution. The \$F(d_1,d_2)\$-distribution has infinite mean for \$d_2 \leq 2\$.

One way of defining the \$F(1,1)\$ distribution is as follows: let \$X_1,X_2\$ be independent \$\mathcal N(0,1)\$ random variables. Then \$\frac{X_1^2}{X_2^2}\sim F(1,1)\$.

Using the \$F\$ distribution comes out 1 byte shorter than the 2 more obvious solutions 1/runif(1) and rcauchy(1).

The plot below shows the evolution of the sample mean, for sample size ranging from 1 to 1e6; you can see that it diverges.

enter image description here


Alternate solution for the same byte count:

rt(1,1)^2

which outputs the square of a realization of Student's distribution \$t(1)\$. It has infinite expected value, since the \$t(1)\$ distribution has infinite variance. The square is needed to guarantee that the output is positive; without it, the expected value is undefined, since the realizations are often enough arbitrarily large positive or negative numbers.

Jelly, 6 bytes

1ḤXḂ$¿

Try it online!

-1 byte thanks to Jonathan Allan

50% chance to output 1, 50% chance to output double this value. -2 bytes using a while loop inspired by Aaron Miller's answer.

Explanation

1
     ¿  While
  ..$
  X     Random integer from 1 to (current value)
   Ḃ    % 2 (this ensures 50% chance no matter how large the current value is - except if it's 1)
 Ḥ      Double the current value

Unfortunately, Jelly doesn't have a "random decimal from 0 to 1" built-in, otherwise this could be <random><reciprocal>.

Proof of validity

The probability to output \$2^x\$ is \$2^{-x-1}\$ for all \$x\in\mathbb{Z}^+\$. Therefore, the expected output is \$\sum\limits_{n=0}^\infty 2^n\cdot 2^{-n-1}=\sum\limits_{n=0}^\infty\frac12\rightarrow\infty\$

Python 3, 31 bytes

from time import*
print(time())

Try it online!

The Python 3 random library uses the current time for a seed. Therefore, it is necessary to specify the random distribution of time that the program is run, to ensure that the simple 1/random() is valid. Depending on the yet-to-come specification, my answer may or may not be valid.

MATL, 3 bytes

1r/

Try it online!

This produces the output 1/x, where x is uniformly distributed on the interval (0,1). This has infinite mean.

Charcoal, 7 bytes

1W‽²↑KA

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

1

Print 1.

W‽²

Repeat a random number of times with an exponential distribution...

↑KA

... duplicate the output.

Bash, 24 bytes

echo 32767/$RANDOM|bc -l

Try it online!

Prints a random number between \$1\$ and \$\infty\$.

Retina, 5 bytes

?+`
1

Try it online! Explanation:

?+`

Loop a random number of times with an exponential distribution...


1

Double the length. (Actually you get 2ⁿ-1, but that's still enough to diverge to infinity.)

Vyxal, 6 7 3 bytes

Thanks to @hyper-neutrino for helping me to understand what expected output is.

Thanks to @Lyxal for porting a different answer than I did for -4 bytes.

∆ṘĖ

Try it Online!

Explanation:

∆Ṙ   # Random float in range [0.0, 1.0)
  Ė  # Reciprocal
     # Implicit output

Pyth, 4 bytes

/1OZ

Try it online!

/1   # 1 divided by
OZ   # Random float between 0 and 1

C++ (gcc), 169 119 118 bytes

#include<bits/stdc++.h>
int main(){std::mt19937 p((uint64_t)new int);std::string s="1";while(p()%2)s+=s;std::cout<<s;}

Try it online!

This solution is directly derived from the linked paradox in the question. The output string is composed of 1 digits so technically it can be interpreted as a number.

The random number generator is seeded with the address of a heap variable. The address changes each time the program is run.

Another option is to use the address of a newly allocated heap variable: mt19937 rng((uint64_t) new char); (source)

Python 3, 37 bytes

from random import*
print(1/random())

Try it online!

I had a slightly cool solution but now this is just the trivial solution.