| Bytes | Lang | Time | Link |
|---|---|---|---|
| 003 | Uiua | 240724T073438Z | Europe20 |
| 026 | Setanta | 240724T035212Z | bb94 |
| 004 | JCram | 240723T134944Z | Kamila S |
| 005 | APL | 220713T165112Z | Vadim Tu |
| 007 | Befunge93 | 210501T204509Z | user1011 |
| 029 | Thue | 210501T093339Z | Conor O& |
| 005 | Random Brainfuck | 210503T195122Z | Helena |
| 009 | R | 210501T101102Z | Dominic |
| 003 | ><> | 210502T205125Z | SE - sto |
| 009 | R | 210501T095839Z | Robin Ry |
| 006 | Jelly | 210501T054457Z | hyper-ne |
| 031 | Python 3 | 210501T134905Z | Trebor |
| 003 | MATL | 210501T105545Z | Luis Men |
| 007 | Charcoal | 210501T082509Z | Neil |
| 024 | Bash | 210501T075735Z | Noodle9 |
| 005 | Retina | 210501T074521Z | Neil |
| 003 | Vyxal | 210501T063243Z | Aaroneou |
| 004 | Pyth | 210501T072058Z | Manish K |
| 118 | C++ gcc | 210501T054445Z | Huỳnh Tr |
| 037 | Python 3 | 210501T050930Z | hyper-ne |
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?.#*@#
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.
R, 9 bytes
1/rexp(1)
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
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)
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.
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Ḃ$¿
-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())
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/
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.
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.
∆ṘĖ
Explanation:
∆Ṙ # Random float in range [0.0, 1.0)
Ė # Reciprocal
# Implicit output
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;}
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())
I had a slightly cool solution but now this is just the trivial solution.
