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.
16
votes
XOR of independent Bernoulli variables
Haskell, 23 bytes
foldr1$ \x p->x+p-2*p*x
Try it online!
If you have two events with probabilities x and ...
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, ...
10
votes
Output a random value from the last 16 values outputted
Python, 102 88 86 85 84 bytes
from random import*;*t,=range(16)
while~-len({*(x:=t[-16:])}):t+=choice(x),
print(t)
Attempt This Online!
-14 thanks to mousetail
-1 ...
10
votes
Output a random value from the last 16 values outputted
Ruby, 55 51 48 bytes
puts x=[*1..16];_,*x=x<<p(x.sample)while(x|x)[1]
Try it online!
9
votes
Implement a bag without replacement
Vyxal, 5 bytes
Inspired by Kevin Cruijssen's 05AB1E answer
6 bytes without the bonus:
{7Þ℅⟑,
Try it Online!
Explanation
...
9
votes
XOR of independent Bernoulli variables
Python 2, 39 bytes
x=0
for p in input():x+=p-x*p*2
print x
Try it online!
Also 39 bytes:
...
8
votes
Implement a bag without replacement
R, 50 40 bytes
-10 bytes thanks to inspiration from Giuseppe
{b=n=1;\()(b<<-c(b,sample(7)))[n<<-n+1]}
Attempt This Online!
A reuseable function that on ...
8
votes
Implement a bag without replacement
JavaScript (ES6), 50 bytes
A function that draws one piece at a time.
m=f=_=>m^(m|=1<<(i=Math.random()*7))?-~i:f(m%=127)
Try it online!
Commented
...
8
votes
8
votes
Output a random value from the last 16 values outputted
Vyxal, 15 13 12 9 bytes
k6≬℅+Ḣ↔ƒ⋎
Try it Online!
Outputs characters from the string "0123456789abcdef"
-3 thanks to ...
8
votes
Output a random value from the last 16 values outputted
R, 66 61 bytes
x=Map(show,L<-1:16)
while(sd(L))L=c(L[-1],print(sample(L,1)))
Try it online!
Explanation outline:
Map the <...
8
votes
Output a random value from the last 16 values outputted
R, 52 bytes
a=b=16:1;while(sd(a[b]))a=c(sample(a[b],1),a);rev(a)
Try it online!
Full program that yields an R vector containing the sequence of values.
R displays ...
8
votes
A randomly colored walk
Wolfram Language (Mathematica), 96 bytes
(d=#2;r={0,255};NestList[⌊RandomPoint[Sphere[#,d],1,{r,r,r}][[1]]⌋&,RandomInteger[r,3],#1])&
Try it online!
...
8
votes
Accepted
Elect the Doge of Venice
05AB1E, 24 bytes
•10¢–‘Þ}Ò•46вvNÉiK}.ry£=
Print each intermediate result as a list on a separated line to STDOUT.
Try it online.
Explanation:
...
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
Halt with 50% non-Halting
Random Brainfuck, 5 bytes
?[--]
Try it online!
Random BF extends BF's set of primitives with the ?, which generates a random ...
7
votes
Halt with 50% non-Halting
Lost, 2 bytes
%@
Try it online!
Although this is apparently a popcon, I figure the language itself is adequately interesting!
Lost is a fairly standard toroidal 2D ...
7
votes
Implement a bag without replacement
Raku, 23 bytes
{grab $||=SetHash(^7):}
Try it online!
Ungolfed:
{
state $bag ||= SetHash(0..6);
$bag.grab;
}
This is a ...
7
votes
Create a bingo card generator
Vyxal J, 29 bytes
(‛₆ṫ⇧Ṅ75ɾ15ẇƛÞ℅5Ẏ;∩2≬2‛λċȦV⁋¶
Try it Online!
Explained (old)
...
7
votes
Output a random value from the last 16 values outputted
Brachylog, 18 bytes
15⟦ẉ₂ᵐ{=|ṛRẉ&b,R↰}
Try it online!
...
7
votes
Output a random value from the last 16 values outputted
JavaScript (ES7), 74 bytes
Same algorithm as the other version, but using the weirder set "5368709124,false".
...
7
votes
Generate an emoticon
Japt -P, 43 bytes
"'*;^` -coux $+-Oox~ ,._"¸ò ö
mö
Vp[VU]ö Îö
Saved four thanks to Shaggy
Try it or generate 10 ...
7
votes
XOR of independent Bernoulli variables
BQN, 8 6 bytesSBCS
Based on xnor's Haskell answer, 2 bytes saved by att and BQN's extensions of logical functions to real numbers.
(∨-∧)´
Run online!
Spells the ...
7
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:
...
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 ...
7
votes
Be big more often
Vyxal, 4 bytes
℅?℅∴
Try it Online!
Return the highest of two random picks
...
6
votes
6
votes
Implement a bag without replacement
05AB1E, 9 bytes
[7L.r)˜ć,
Uses a bag with integers [1,2,3,4,5,6,7]. Outputs the random piece indefinitely on separated newlines....
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
random × 345code-golf × 319
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