[Question]
  [
Your task is simple: output the letter `x` a random number of times. Every possible length of `x`s must have a non-zero probability of being output.
Of course, there must be some lengths whose probabilities tend to \$ 0 \$, in order for the total probabilities to sum to \$ 1 \$, but all must still be theoretically possible.
* You may choose whether to include the empty string as a possible output
* You may have any consistent separator between the `x`s, and you may output the separator more than once. The separator may not contain the unary character
* You may use any other consistent single character instead of `x`
* Per standard rules, [your program must always halt](https://codegolf.meta.stackexchange.com/a/4785) in finite time. (Terminating "with probability 1" [is allowed](https://codegolf.meta.stackexchange.com/q/17122), though)
* You can assume your language's random number generator is perfectly random. If it is supposedly continuous, you may assume it has infinite precision.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code wins.
  
[Answer]
# [><>](https://esolangs.org/wiki/Fish), 3 bytes
```
x7n
```
Terminates on an error once x sends the IP left.
[Try it online!](https://tio.run/##S8sszvj/v8I87/9/AA "><> – Try It Online")
~~4 bytes:
```
x7
n
```
[Try it online!](https://tio.run/##S8sszvj/v8KcK@//fwA "><> – Try It Online")
This will terminate with an error when `n` has been executed more times than `7`, which has probability 1 of eventually occurring, thought there is no upper limit on how many times `7` will be executed first.~~
[Answer]
# MATLAB, ~~28~~ 25 bytes
```
while rand<.5 disp(1),end
```
Outputs "1" a random number of times, with newline in between. Each length l of has half the probability of length l-1.
Thanks @Luis Mendo for the 3 bytes!
[Answer]
# [Vyxal 2.4.1](https://github.com/Vyxal/Vyxal), 6 bytes
```
{×₴₀℅|
```
[Try it Online!](https://lyxal.pythonanywhere.com?flags=&code=%7B%C3%97%E2%82%B4%E2%82%80%E2%84%85%7C&inputs=&header=&footer=)
We use v2.4.1 because it seems in 2.6 and later there's no way to get a random bit in two bytes.
```
{      # Forever...
 ×₴    # Print an asterisk
     | # While...
   ₀℅  # A random bit is nonzero
```
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 43 bytes
```
setrand(getwalltime)
while(random,print(1))
```
(Don't) [Try it online!](https://tio.run/##K0gsytRNL/j/vzi1pCgxL0UjPbWkPDEnpyQzN1WTqzwjMydVAySen6tTUJSZV6JhqKn5/z8A "Pari/GP – Try It Online") This version is very likely to time out on TIO; here's a slightly longer version that produces nicer results: [Try it online!](https://tio.run/##K0gsytRNL/j/vzi1pCgxL0UjPbWkPDEnpyQzN1WTqzwjMydVAySen6tqqVNQlJlXomGoqfn/PwA "Pari/GP – Try It Online")
### Explanation
```
setrand(getwalltime)
```
Seed the random number generator with `getwalltime`, which is "time in ms since UNIX Epoch."
```
while(random,print(1))
```
While a random integer in the range \$[0,2^{31})\$ is not equal to zero, print `1` with a trailing newline.
[Answer]
# [R](https://www.r-project.org/), 20 bytes
```
while(rexp(1))cat(1)
```
[Try it online!](https://tio.run/##K/r/vzwjMydVoyi1okDDUFMzObEESP3/DwA "R – Try It Online")
3 bytes longer than [Giuseppe's R answer](https://codegolf.stackexchange.com/a/243531/95126), but can output unary values greater than 2147483647.
In fact, it will nearly *always* output unary values that are significantly greater than 2147483647, since the chance of stopping after any outputted `1` character is about `1e-324` (the lower limit of [R](https://www.r-project.org/)'s double-precision numeric type, below which values are truncated to zero).  
This will also nearly always exceed the output limit on TIO, so [here](https://tio.run/##ZY/BTsQwDETv/YqR9rCNVCoCnCr1W1BIXGopJFXibhd@viSrpRJwsuOM53nSfoKjiQPBxw1LIsuZY8CF0q3GCedE1@WMaQ1Wyqg5oWVHQdgaD4nIYoIzyf0VdnhbBRvLjBj8J3QhWf4oS4s3lqr1weuKa464GL9ShqecIbMJeOw1TCKkuAZHruK@KMUOHLKQcdXk35Kmh@enFzU0Tcn0ejDGn8Pa0nSO31nyqNUxvXZ936sbqSru77tQNTXc@MuwLcklD0P9Ufs2s6e29q1WyhopZd@/AQ) is a link using a modified version of the `rexp` function with only 1 decimal place of precision.
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf/blob/master/math_golf.txt), ~~4~~ 3 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py)
```
⌂v▲
```
Pushes `*` with a probability of \$\frac{4\text{,}294\text{,}967\text{,}294}{4\text{,}294\text{,}967\text{,}295}\$ (99.999999999767%) each iteration, and won't include the empty output (so will always output at least one `*`).
*Don't try it online.*
**Previous 4 [byter](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py):**
```
⌂v¶▼
```
Pushes `*` with a probability of \$\frac{837\text{,}973\text{,}946}{858\text{,}993\text{,}459}\$ (~97.55%) each iteration, and won't include the empty output (so will always output at least one `*`).
[Try it online.](https://tio.run/##y00syUjPz0n7//9RT1PZoW2Ppu35/x8A)
**Explanation:**
```
  ▲   # Do while falsey with pop:
⌂     #  Push character '*'
 v    #  Push a random integer in the range [-2³¹, 2³¹)
      #  (only 0 is a falsey integer in MathGolf)
      # (after which the entire joined stack is output implicitly as result)
```
```
   ▼  # Do while truthy with pop:
⌂     #  Push character '*'
 v    #  Push a random integer in the range [-2³¹, 2³¹)
  ¶   #  Pop and check if this integer is a (positive) prime number
      # (after which the entire joined stack is output implicitly as result)
```
The mentioned probability is basically the amount of non-prime numbers within the range \$[-2^{31},2^{31})\$ (which is \$4\text{,}189\text{,}869\text{,}730\$ [according to WolframAlpha](https://www.wolframalpha.com/input?i=2%5E31+%2B+2%5E31-1+-+%28count+primes+less+than+2%5E31%29)) as numerator and total amount of integers within the range \$[-2^{31},2^{31})\$ (basically \$2^{32}-1=4\text{,}294\text{,}967\text{,}295\$) as denominator (and then simplified by dividing both by their greatest common divisor \$5\$).
[Answer]
# [Pip](https://github.com/dloscutoff/pip), 4 bytes
```
1X/r
```
Outputs one or more `1`s (usually not very many of them). [Attempt This Online!](https://ato.pxeger.com/run?1=m724ILNgwYKlpSVpuhZLDCP0iyBMqAhMBgA)
### Explanation
```
   r  Random number in [0, 1)
  /   Invert
1X    Repeat 1 that many times
```
Theoretically, I think it's possible for this to output nothing if `r` returns exactly `0`, but that's within the rules anyway.
[Answer]
# [Haskell](https://www.haskell.org/), 56 bytes
```
import System.Random
main=print 1>>randomIO>>=([main]!!)
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/PzO3IL@oRCG4srgkNVcvKDEvJT@XKzcxM8@2oCgzr0TB0M6uCCzo6W9nZ6sRDZKKVVTU/P8fAA "Haskell – Try It Online")
Random improvements applied to [AZTECCO's answer](https://codegolf.stackexchange.com/a/243550/78410). Halts by non-recoverable error, so it needs to be a full program. Prints at least once, but has \$\frac{1}{2^{64}}\$ chance of continuing to print. (The return type of `randomIO` is inferred to be `Int`, which is a signed 64-bit integer in TIO's environment.)
# [Haskell](https://www.haskell.org/), 59 bytes
```
import System.Random
f=do print 1;n<-randomIO;mapM_ id[f|n]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/PzO3IL@oRCG4srgkNVcvKDEvJT@XK802JV@hoCgzr0TB0DrPRrcILOzpb52bWOAbr5CZEp1Wkxf7PzcxM8827T8A "Haskell – Try It Online")
A function that terminates gracefully and has 1/2 chance of continuing. `[f|n]` becomes `[f]` or `[]` with 1/2 chance each, and `mapM_ id` (one byte shorter than `sequence_`) runs all monads in the list sequentially. Deleting `_` results in a type inference error.
[Answer]
# [Thue](https://esolangs.org/wiki/Thue), 20 bytes
```
x::=xx
x::=~x
::=
x
```
[Try it online!](https://tio.run/##K8koTf3/v8LKyraiggtE1VVwAUmuCq7//wE "Thue – Try It Online") (I'm not sure why the final blank line is necessary, but the program doesn't output anything if I delete it.)
### Explanation
Thue starts with an initial string and executes rewriting rules nondeterministically until it cannot execute any more, at which point it halts. This program consists of two rules:
```
x::=xx
```
Replace an `x` in the string with `xx`.
```
x::=~x
```
Delete an `x` from the string and output `x`.
The program's starting string is:
```
x
```
Thus, at each stage of execution, the string consists of one or more `x`s; it can either get longer by one `x`, or get shorter by one `x` and output an `x`. Once all the `x`s have been output, the program halts. This occurs with probability 1, although in practice sometimes the interpreter segfaults.
[Answer]
# [Random Brainfuck](https://github.com/TryItOnline/brainfuck), ~~7~~ 6 bytes
*-1 byte thanks to [je je](https://codegolf.stackexchange.com/users/103412/je-je)'s observation that Null bytes are accebtale as output.*
```
+[>.?]
```
[Try it online!](https://tio.run/##K0rMS8nP1U0qSszMSytNzv7/XzvaTs8@9v9/AA "Random Brainfuck – Try It Online")
Outputs at least 1 `0x01` character, and terminates each iteration with a 1/256 chance. `?` sets the current cell to a random byte, and this will only terminate when that byte is 0.
## Detailed Explanation
```
+       set first cell to 1
[       while the current cell is non-zero:
  >         move right one cell
  .         output it (0x00)
  ?         set current cell to a random byte from 0 to 255
]       end while
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 29 bytes by noodle man
```
_=>''.padEnd(1/Math.random())
```
[Try it online!](https://tio.run/##BcExDoAgDADA3Y/Yxog4I26OvsE0AorB1iDx@3h30UfvnuNTehbna7B1s3Pbqofcwg7GYaVyqkzs5AbEapogGaLVJk6j1qbrIu7CrySvkhwQAFXyfJQT6w8 "JavaScript (Node.js) – Try It Online")
# [JavaScript (Node.js)](https://nodejs.org), 30 bytes
```
f=_=>Math.random()>.5?'':1+f()
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNHNy09J/f8/zTbe1s43sSRDrygxLyU/V0PTTs/UXl3dylA7TUPzvzVXWn6RRqatgXWmjaGBgbW2dqZmcn5ecX5Oql5OfroGUI3mfwA "JavaScript (Node.js) – Try It Online")
Simple
[Answer]
# [R](https://www.r-project.org/), ~~19~~ 17 bytes
```
strrep(1,rexp(1))
```
[Try it online!](https://tio.run/##K/r/v7ikqCi1QMNQpyi1Akhpav7/DwA "R – Try It Online")
Samples from an \$Exp(1)\$ distribution to determine the length. This allows any positive real number to be generated, which `strrep` truncates, though most of them will be rather small.
[Answer]
# [Factor](https://factorcode.org/) + `random.c`, ~~26~~ ~~25~~ 21 bytes
```
[ 1 . rand 9 > ] loop
```
[Try it online!](https://tio.run/##S0tMLskv@h8a7OnnbqVQUJRaUlJZUJSZV6JQlJiXkp@rl6yQm1iSoZCdWpSXmqNg/T9awVBBDyypYKlgpxCrkJOfX/D/PwA "Factor – Try It Online")
This uses similar logic as flipping a coin until it lands on tails. Only you're using a severely weighted coin, so you usually get heads, leading to fairly long runs. `rand` is C's `rand()` function, returning an integer between 0 and 2147483647 (probably, though it doesn't matter). Each iteration of the loop, print `1` followed by a newline. If the output of `rand` is greater than 9, perform another iteration; otherwise, end the program.
[Answer]
# Pure [Bash](https://www.gnu.org/software/bash/) (no external utilities), 30 26
```
for((;RANDOM;));{ echo x;}
```
[Try it online!](https://tio.run/##S0oszvhf/T8tv0hDwzrI0c/F39daU9O6WiE1OSNfocK69n9tTXnyfwA "Bash – Try It Online")
[Answer]
# Python 3, ~~62~~ ~~60~~ ~~59~~ ~~45~~ ~~43~~ 41 bytes
```
import os
while os.urandom(1)[0]:print(1)
```
[Try online!](https://ato.pxeger.com/run?1=m72soLIkIz9vwYKlpSVpuhY3NTNzC_KLShTyi7nKMzJzUoEMvdKixLyU_FwNQ81og1irgqLMvBIgG6IBqg-mHwA)
Thanks to @pxeger, @mvirts, @MarcMush and @loopywalt.
[Answer]
# [Python 3](https://docs.python.org/3/), 50 bytes
```
lambda:'x'*int(1/(1-random()))
from random import*
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72soLIkIz9vweI025ilpSVpuhY3jXISc5NSEq3UK9S1MvNKNAz1NQx1ixLzUvJzNTQ1NbnSivJzFSB8hczcgvyiEi2oToW0_CKFTIXMPJB0eqqGoammFZeCQkERyJg0oGaIugULIDQA)
*Thanks to users `pxeger` and `DominicVanEssen` for their clarifications.*
Outputs the character `x` a random number of times. Random sequences of `x`s are separated by the newline character.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~7~~ 6 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
2ȮX$’¿
```
A niladic Link that prints `2`s as a side-effect (it also yields `1`). Only prints strictly positive numbers (as allowed in the specification).
**[Try it online!](https://tio.run/##y0rNyan8/9/oxLoIlUcNMw/t/39oUdr//wA "Jelly – Try It Online")** (The footer suppresses the printing of `1` that a full program would otherwise do implicitly.)  
Or [see forty at once](https://tio.run/##ASAA3/9qZWxsef//MsiuWCTigJnCv//CouG4nzHhuYQp//80MA) (plus a single trailing newline).
### How?
```
2ȮX$Ḋ¿ - Link: no arguments
2      - two - let's call this V
     ¿ - while...
    ’  - ...condition: decrement V (V=2 -> 1 (truthy); V=1 -> 0 (falsey))
   $   - ...do: last two links as a monad - f(V):
 Ȯ     -   print V, yield V
  X    -   random integer in [1,V] -> next V = 1 or 2 (probability = 0.5)
```
---
Previous @ 7 bytes:
```
2XȮß$Ị¡
```
A niladic Link that prints `1`s as a side-effect (it also yields `2`). This one includes the empty output (i.e. zero).
[Try it online!](https://tio.run/##ARkA5v9qZWxsef//MljIrsOfJOG7isKh/8KiZv// "Jelly – Try It Online") (The footer suppresses the printing of `2` that a full program would otherwise do implicitly.)
[Answer]
# [MATL](https://github.com/lmendo/MATL), 5 bytes
```
`1rEk
```
Outputs `1` separated by newlines `n` times, where `n` is a (shifted) geometric random variable with parameter `1/2`. This means `n` is `1,2,3...` with probability `1/2`,`1/4`, `1/8` ... The program halts with probability `1`.
[**Try it online!**](https://tio.run/##y00syfn/P8GwyDX7/38A "MATL – Try It Online")
```
`     % Do...while
  1   %   Push 1
  r   %   Push uniform random number in the interval (0,1)
  E   %   Multiply by 2
  k   %   Round down. This gives 0 or 1 with probability 1/2
      % End (implicit). The top of the stack is used as loop condition
      % If 1 a new iteration is executed, otherwise the loop is exited
      % Display stack (implicit)
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 5 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
[?₄Ω#
```
Outputs `0`s with a probability of \$\frac{3}{4}\$ each time. Could be a probability of \$\frac{2}{3}\$ or \$\frac{1}{2}\$ for the same byte-count by replacing the `₄` with `т` or `T` respectively.
[Try it online.](https://tio.run/##yy9OTMpM/f8/2v5RU8u5lcr//wMA)
**Explanation:**
```
[      # Loop indefinitely:
 ?     #  Print an empty string without newline in the first iteration,
       #  or the 0 that was previously on the stack in other iterations
  ₄    #  Push 1000 (or 100 for `т` or 10 for `T`)
   Ω   #  Pop and push a random digit from this integer
    #  #  If it's 1: stop the infinite loop
```
[Answer]
# [Cascade](https://github.com/GuyJoKing/Cascade), 6 bytes
```
$/
.x/
```
[Try it online!](https://tio.run/##S04sTk5MSf3/X0WfS69C//9/AA "Cascade – Try It Online")
# [Cascade](https://github.com/GuyJoKing/Cascade), 6 bytes
```
\$
.|x
```
[Try it online!](https://tio.run/##S04sTk5MSf3/P0aFS6@m4v9/AA "Cascade – Try It Online")
*Likely* optimal, considering one row has to have width at least 3 for `$` to produce distinct random outcomes.
Both programs consist of two random branches from `$`, one of which returns the codepoint of `x`, and the other of which prints (the character value of) and returns the return value of (a fresh evaluation of) the `$`.
[Answer]
# TI-Basic (TI-84), ~~11~~ 10 [bytes](https://codegolf.meta.stackexchange.com/a/4764/98541)
```
Repeat checkTmr(startTmr
Disp 1
End
```
the program can only end when the check `checkTmr(startTmr` happens between two seconds (`checkTmr(startTmr) = 1` instead of `0`). This way, any number of ones is possible (the shortest I got was 3 ones, the longest printed during approx 10 seconds)
`Repeat` always executes the loop the first time, so the empty string is not included (`repeat until` or `do while not`)
For calculators without time (TI-83), replace `checkTmr(startTmr` with `rand>.9` but it's not true randomness (same output on each reset of the calculator)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 4 bytes
```
Ｗ‽φx
```
[Try it online!](https://tio.run/##S85ILErOT8z5/788IzMnVUEjKDEvJT9Xw9DAwEBTUyGgKDOvREOpQknT@v///7plOQA "Charcoal – Try It Online") Link is to verbose version of code. Prints an average of 999 `x`s. (Other average counts from `1` to `9` are also possible by substituting an appropriate character for `φ`.)
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 29 bytes
```
f(){putchar(63)&rand()&&f();}
```
[Try it online!](https://tio.run/##FYyxDoQgEER7vmJjQXZjTIgmNtz9iQ1B0S1Eg1x14dtxKefNm/HD7n2tAel//7I/XMJ5Ip1cXJG0Fm5L5ZjhdBxBLPW0DjDzuaEhsipcCbAp/DWWP6Oxfc8iQhsrkNsHuyV2Eooq9QU "C (gcc) – Try It Online")
[Answer]
# [J](http://jsoftware.com/), ~~10~~ 9 bytes
```
1#~>.@%?0
```
-1 byte by using Reflex `~` instead of Bond `&1`
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/DZXr7PQcVO0N/mtycaUmZ@QrpP0HAA "J – Try It Online")
[Answer]
## Batch, 28 bytes
```
@if %random% gtr 9 echo x&%0
```
Outputs an average of about 3276 `x`s, assuming `CMD.EXE` doesn't stack overflow first.
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 7 bytes
```
9w9ṙ9|↰
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/37Lc8uHOmZY1j9o2/P//HwA "Brachylog – Try It Online")
### Explanation
```
9w         Write 9
  9ṙ       Pick a random integer in 0..9
    9      If it is 9, terminate
     |↰    Else, recurse
```
[Answer]
# [Alchemist](https://github.com/bforte/Alchemist), 14 bytes
```
_->Out__+_
_->
```
[Try it online!](https://tio.run/##S8xJzkjNzSwu@f8/XtfOv7QkPl47ngvI/P8fAA "Alchemist – Try It Online")
Breaks the loop with probability \$\frac12\$ at each iteration.
### Explanation
The program starts with a single `_` atom. Since both instructions consume only a `_` atom, they are both satisfied and one is chosen randomly. The first outputs the number of `_` atoms remaining (0) and produces another `_` atom to continue the loop. The second just consumes the atom, ending the loop.
[Answer]
# [Julia 1.0](http://julialang.org/), 22 bytes
```
!_=.1<rand()&&!show(1)
```
[Try it online!](https://tio.run/##yyrNyUw0rPj/XzHeVs/QpigxL0VDU01NsTgjv1zDUPN/Wn6RQqZCZp6CoZWhgQGXYl5@SUZmXjpXQVFmXklOnoYmV2peyn8A "Julia 1.0 – Try It Online")
[Answer]
# [Java](https://en.wikipedia.org/wiki/Java_(programming_language)), ~~58~~ ~~45~~ 44 bytes
```
String f(){return.5<Math.random()?"":1+f();}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70kK7EsccHN2ILSpJzMZIXknMTiYgXfxMy8aqhIcUliCZAqy89MUcgFimsElxRl5qVHxyZqVnMFVxaXpObq5ZeW6BUARUty8jTSNDQ1rWu5UHQvLS1J07W4qQPRqgBUUl2UWlJalKdnauObWJKhV5SYl5Kfq6Fpr6RkZagNlLeuhehZCKUXLIDQAA)
-1 thanks to @Kevin Cruijssen
Same strategy as the [NodeJS](https://codegolf.stackexchange.com/a/243526/107299) answer. Previous answer did not fit challenge spec, this version turned out to be shorter as well.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), ~~28~~ 24 bytes
```
Dot@@Table[x,1/Random[]]
```
Thanks to att for –4!
[Try it online!](https://tio.run/##y00syUjNTSzJTE78X6RgZfvfJb/EwSEkMSknNbpCx1A/KDEvJT83OjYWKBEdUJSZVxJdFKujYGgQ@x8A "Wolfram Language (Mathematica) – Try It Online")
]