g | x | w | all
Bytes Lang Time Link
004Japt h250130T174022ZShaggy
047JavaScript Node.js230907T094357Zl4m2
044Perl 5 pa231108T162849ZXcali
011Uiua231108T145611ZPseudo N
004MathGolf230908T094138ZKevin Cr
00505AB1E230908T093812ZKevin Cr
004Vyxal230903T162817ZDominic
027R230903T094113ZDominic
019Wolfram Language Mathematica230905T060037ZGreg Mar
003Jelly230904T165305Zpxeger
041JavaScript Node.js230903T085514ZMTN
010Charcoal230903T071418ZNeil
013Raku230904T050536ZJo King
024Ruby230903T095736ZG B
004Jelly230903T201412ZJonathan
058JavaScript Node.js230903T072340ZJoyal Ma
008J230903T074112ZJonah
037Julia230903T143634ZMarcMush
035Factor230903T113956Zchunes
048WolframLanguage230903T075454Zinfinite
064Python230903T080834Zbsoelch
003Vyxal230903T072847Zlyxal
041Python230903T071017ZMTN

Japt -h, 4 bytes

ö2 Í

Try it

ö2 Í     :Implicit input of array
ö2       :Select 2 random elements, with repetition
   Í     :Sort
         :Implicit output of last element

JavaScript (Node.js), 47 bytes

a=>a.sort((a,b)=>b-a)[.4/Math.random()|0]||a[0]

Try it online!

JavaScript (V8), 53 bytes

a=>Math.max(...a.map(_=>a[a.length*Math.random()|0]))

Try it online!

Perl 5 -pa, 44 bytes

@b=map{($_)x++$,}sort{$a-$b}@F;$_=$b[rand@b]

Try it online!

Uiua, 11 bytes

⊡⌊×↥⚂⚂⧻.⊏⍏.

Try it online!

Built on a similar principle to lots of others others (randomly pick two items and use the max), but tweaked slightly

Explanation

        ⊏⍏. # sort the array
      ⧻.    # get its length
   ↥⚂⚂      # max of two random numbers [0,1)
  ×         # multiply to scale to [0,len)
⊡⌊          # floor and index into array

MathGolf, 4 bytes

‼ww╙

Try it online.

Port of @DominicVanEssen's second R answer.

Explanation:

‼     # Apply the next two builtins separately on the current stack:
 w    #  Pop and push a random item from the given (implicit) input-list
  w   #  Same
   ╙  # Pop the top two items on the stack, and leave its maximum
      # (after which the entire stack is output implicitly as result)

05AB1E, 5 bytes

{.s˜Ω

Try it online.

Or alternatively, a port of @DominicVanEssen's second R answer is 5 bytes as well:

ΩIΩ‚à

Try it online.

Explanation:

{      # Sort the (implicit) input-list from lowest to highest
 .s    # Pop and push a list of its suffixes
   ˜   # Flatten it
    Ω  # Pop and push a random item from this list
       # (which is output implicitly as result)
Ω      # Push a random item from the (implicit) input-list
 I     # Push the input-list
  Ω    # Pop and push another random item from it
   ‚   # Pair the two together
    à  # Pop and push its maximum
       # (which is output implicitly as result)

Vyxal, 4 bytes

℅?℅∴

Try it Online!

Return the highest of two random picks

℅       # Random choice of single item from input array
 ?      # Input (again)
  ℅     # Random choice of single item from this
    ∴   # Maximum of these two values

R, 27 bytes

(Or 20 bytes in R>=4.1 using \ instead of function)

function(x)-sample(-x,1,,x)

Try it online!

The 4th argument of R’s sample function is prob=“a vector of probability weights for obtaining the elements of the vector being sampled”.

Unfortunately, a “convenience” feature of sample changes its behaviour when its first argument is a single positive number, so we need to negate it here to avoid this, and then re-negate the result.

——

R, 31 bytes

(Or 24 bytes in R>=4.1 using \ instead of function)

function(x)max(-sample(-x,2,T))

Try it online!

Return the highest of two random picks.

Wolfram Language (Mathematica), 19 bytes

RandomChoice[#->#]&

Try it online! Like other languages, the builtin can use weights, so the input can serve as the weights as well. Works with nonnegative real numbers.

Jelly, 3 bytes

X»X

Attempt This Online!

Distribution over 1000 runs: Attempt This Online!

Uses Dominic van Essen's insight: pick a random element (X), twice, then take the larger of them (»).

JavaScript (Node.js), 41 bytes

a=>a.sort()[a.length*Math.random()**.5|0]

Try it online! or run it 100000 times

Takes an Float64Array (so the sorting works properly). Port of bsoelch's Python answer.
+10 bytes to take a regular array.

Charcoal, 10 bytes

FAFι⊞υιI‽υ

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

FA

Loop over the input numbers.

Fι⊞υι

Push each number that many times to the predefined empty list.

I‽υ

Output a random number from that list.

8 bytes using the newer version of Charcoal on ATO:

I‽ΣEAEιι

Attempt This Online! Link is to verbose version of code. Explanation:

    A       Input list
   E        Map over elements
      ι     Current element
     E      Map over implicit range
       ι    Outer element
  Σ         Flatten
 ‽          Random element
I           Cast to string
            Implicitly print

A port of @JoyalMathew's JavaScript answer in straight succinct Charcoal is only 6 bytes:

I⌈∨Φθ‽

Try it online! Explanation:

    θ   Input list
   Φ    Filtered by
     ‽  Random coin flip
  ∨     If empty then implicitly use input list
 ⌈      Take the maximum
I       Cast to string
        Implicitly print

Raku, 13 bytes

*.roll(2).max

Attempt This Online!

Picks two elements independently and returns the max of the two.

Ruby, 32 24 bytes

->l{(l+l).sample(2).max}

Try it online!

Thanks to Dominic van Essen for -4 bytes and the idea to get to -8.

Jelly, 4 bytes

ṢxJX

A monadic Link that accepts a list of distinct floats and outputs a random one.

Try it online! Or see the results of 10,000 runs.

How?

Weights by the count of lesser-or-equal values.

The probability of picking the \$a^{\text{th}}\$ most minor of \$n\$ numbers is \$\frac{2a}{n(n+1)}\$.

For example given [2.5, 2.2, 2.4, 2.6, 2.1, 2.3] (\$n(n+1)=6(6+1)=42\$):
$$P(2.1)=\frac{1}{21}, P(2.2)=\frac{2}{21}, \cdots , P(2.6)=\frac{6}{21}$$

ṢxJX - Link: list of numbers, A
Ṣ    - sort {A} -> [a1, a2, a3, ...]
  J  - indices {A} -> [1, 2, 3, ...]
 x   - {sorted A} times {indices of A} -> [a1, a2, a2, a3, a3, a3, ...]
   X - uniform random choice

JavaScript (Node.js), 58 bytes

a=>a.sort((a,b)=>b-a).filter(_=>Math.random()<.5)[0]||a[0]

Try it online!

Sorts the numbers, filters them randomly, then chooses the largest one.

J, 8 bytes

#~{~1?+/

Attempt This Online!

Julia, 37 bytes

!l=rand([fill.(sort(l),keys(l))...;])

Attempt This Online!

sort the list and repeat each element by their index before randomly choosing a number

with integers only, 25 bytes

!l=rand([fill.(l,l)...;])

Attempt This Online!

directly repeat each number by itself

Factor, 35 bytes

[ [ dup <array> ] map-flat random ]

Attempt This Online!

Create n copies of each n, and select one at random (uniformly).

For instance, { 5 1 2 } becomes { 5 5 5 5 5 1 2 2 }.

WolframLanguage, 63 48 bytes

Thanks to att for shaving of 15

Sort[#][[Floor@Log2[Random[](2^Tr[1^#]2-3)+2]]]&

Original answer:

f[r_]:=Sort[r][[Floor@Log[2,RandomReal[{2,2^(Length@r+1)-1}]]]]

Sorts the array and picks a random number between 2 and 2^(l+1)-1, where l is the length of the list. Then takes the logarithm to get the array index. This gives each number a twice as high probability to be chosen as the previous one.

Example

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

For a run of 2*10^7 repetitions:

{{1, 19562}, {2, 39068}, {3, 78145}, {4, 156309}, {5, 313362}, {6, 625087}, {7, 1249297}, {8, 2504498}, {9, 5005805}, {10, 10008867}}

enter image description here

Python, 64 bytes (also supports negative numbers)

lambda l:sorted(l)[int(len(l)*random()**.5)]
from random import*

Attempt This Online!

Explanation

Sort the list, the pick a random element. The square root biases the result towards the end of the list.

Vyxal, 3 bytes

øḊ℅

Try it Online!

Explained

øḊ℅­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏‏​⁡⁠⁡‌­
øḊ   # ‎⁡Run length decode the input, using the input as both character source and lengths. Basically, zip and run length decode
  ℅  # ‎⁢Choose randomly from that list. 
💎

Created with the help of Luminespire.

Python, 41 bytes

lambda l:choices(l,l)
from random import*

Attempt This Online! or run it 100000 times

Uses the list itself as the weights.