| Bytes | Lang | Time | Link |
|---|---|---|---|
| 004 | Japt h | 250130T174022Z | Shaggy |
| 047 | JavaScript Node.js | 230907T094357Z | l4m2 |
| 044 | Perl 5 pa | 231108T162849Z | Xcali |
| 011 | Uiua | 231108T145611Z | Pseudo N |
| 004 | MathGolf | 230908T094138Z | Kevin Cr |
| 005 | 05AB1E | 230908T093812Z | Kevin Cr |
| 004 | Vyxal | 230903T162817Z | Dominic |
| 027 | R | 230903T094113Z | Dominic |
| 019 | Wolfram Language Mathematica | 230905T060037Z | Greg Mar |
| 003 | Jelly | 230904T165305Z | pxeger |
| 041 | JavaScript Node.js | 230903T085514Z | MTN |
| 010 | Charcoal | 230903T071418Z | Neil |
| 013 | Raku | 230904T050536Z | Jo King |
| 024 | Ruby | 230903T095736Z | G B |
| 004 | Jelly | 230903T201412Z | Jonathan |
| 058 | JavaScript Node.js | 230903T072340Z | Joyal Ma |
| 008 | J | 230903T074112Z | Jonah |
| 037 | Julia | 230903T143634Z | MarcMush |
| 035 | Factor | 230903T113956Z | chunes |
| 048 | WolframLanguage | 230903T075454Z | infinite |
| 064 | Python | 230903T080834Z | bsoelch |
| 003 | Vyxal | 230903T072847Z | lyxal |
| 041 | Python | 230903T071017Z | MTN |
Japt -h, 4 bytes
ö2 Í
ö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]
JavaScript (V8), 53 bytes
a=>Math.max(...a.map(_=>a[a.length*Math.random()|0]))
Uiua, 11 bytes
⊡⌊×↥⚂⚂⧻.⊏⍏.
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╙
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˜Ω
Or alternatively, a port of @DominicVanEssen's second R answer is 5 bytes as well:
ΩIΩ‚à
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
℅?℅∴
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)
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))
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
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
Ruby, 32 24 bytes
->l{(l+l).sample(2).max}
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]
Sorts the numbers, filters them randomly, then chooses the largest one.
J, 8 bytes
#~{~1?+/
1?+/Sum the numbers and then choose a random index between 0 and the sum#~{~Take that index from a new array formed by duplicating each number as many times as itself. Eg,#~ 1 2 3gives1 2 2 3 3 3, and we choose a random index from the interval \$[0,6)\$.
Julia, 37 bytes
!l=rand([fill.(sort(l),keys(l))...;])
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)...;])
directly repeat each number by itself
Factor, 35 bytes
[ [ dup <array> ] map-flat random ]
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}}
Python, 64 bytes (also supports negative numbers)
lambda l:sorted(l)[int(len(l)*random()**.5)]
from random import*
Explanation
Sort the list, the pick a random element. The square root biases the result towards the end of the list.
Vyxal, 3 bytes
øḊ℅
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.
