25
votes
Be big more often
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.
14
votes
11
votes
Be big more often
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, ...
8
votes
Be big more often
J, 8 bytes
#~{~1?+/
Attempt This Online!
1?+/ Sum the numbers and then choose a random index between 0 and the sum
...
7
votes
Be big more often
Vyxal, 4 bytes
℅?℅∴
Try it Online!
Return the highest of two random picks
...
7
votes
Be big more often
JavaScript (Node.js), 41 bytes
a=>a.sort()[a.length*Math.random()**.5|0]
Try it online! or run it 100000 times
Takes an ...
6
votes
Be big more often
WolframLanguage, 63 48 bytes
Thanks to att for shaving of 15
Sort[#][[Floor@Log2[Random[](2^Tr[1^#]2-3)+2]]]&
Original answer:
...
5
votes
Be big more often
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.
4
votes
Be big more often
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, ...
4
votes
Be big more often
Charcoal, 10 bytes
FAFι⊞υιI‽υ
Try it online! Link is to verbose version of code. Explanation:
FA
Loop over the input numbers.
<...
4
votes
Be big more often
Wolfram Language (Mathematica), 19 bytes
RandomChoice[#->#]&
Try it online! Like other languages, the builtin can use weights, so the input can serve as the ...
4
votes
Be big more often
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 (...
4
votes
Be big more often
Raku, 13 bytes
*.roll(2).max
Attempt This Online!
Picks two elements independently and returns the max of the two.
3
votes
Be big more often
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
...
3
votes
Be big more often
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, <...
3
votes
Be big more often
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 ...
1
vote
1
vote
Make your code error, but only sometimes!
Volatile, 3 bytes
~~/
~ pushes a random number onto the stack (which may include zero), and ...
1
vote
1
vote
Randomize the scalars of an array
Factor, 25 bytes
[ dup [0,b] swap sample ]
Try it online!
...
1
vote
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
random × 344code-golf × 318
string × 61
ascii-art × 39
number × 23
game × 23
array × 16
math × 13
generation × 13
grid × 11
graphical-output × 11
permutations × 11
kolmogorov-complexity × 10
code-challenge × 9
combinatorics × 9
probability-theory × 9
popularity-contest × 8
geometry × 8
sequence × 7
integer × 7
animation × 7
arithmetic × 5
sorting × 5
open-ended-function × 5
restricted-source × 4