New answers tagged random
0
votes
Randomize the scalars of an array
RProgN 2, 3 bytes
0Rñ
Explained
0Rñ
0R # Range of numbers 0-n (Implicit input)
ñ # Shuffle, Implicit output.
Try it online!
0
votes
Output a random unary string
Volatile, 7 bytes
~(:-.~)
Assumes that the . instruction is present in the interpreter. Prints a random length of ...
1
vote
Make your code error, but only sometimes!
Volatile, 3 bytes
~~/
~ pushes a random number onto the stack (which may include zero), and ...
0
votes
Be big more often
MathGolf, 4 bytes
‼ww╙
Try it online.
Port of @DominicVanEssen's second R answer.
Explanation:
...
0
votes
Be big more often
05AB1E, 5 bytes
{.s˜Ω
Try it online.
Or alternatively, a port of @DominicVanEssen's second R answer is 5 bytes as well:
ΩIΩ‚à
...
0
votes
Be big more often
JavaScript (V8), 53 bytes
a=>Math.max(...a.map(_=>a[a.length*Math.random()|0]))
Try it online!
Not best now though
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
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 ...
7
votes
Be big more often
Vyxal, 4 bytes
℅?℅∴
Try it Online!
Return the highest of two random picks
...
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, <...
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.
14
votes
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 ...
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, ...
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:
...
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
...
1
vote
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.
<...
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.
Top 50 recent answers are included
Related Tags
random × 344code-golf × 318
string × 61
ascii-art × 39
number × 23
game × 23
array × 16
math × 13
generation × 13
graphical-output × 11
grid × 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