Skip to main content
31 votes

What an Odd Function

Jelly, 7 bytes Æf^<¥4P Swaps 2s and 3s in the input's prime factorization. The probability of odds is 2/3. Try it online! How it works ...
Dennis's user avatar
  • 210k
26 votes
Accepted

Pick a random number between 0 and n using a constant source of randomness

x86 machines with rdrand instruction, 10 bytes ...
Margaret Bloom's user avatar
21 votes

Pick a random number between 0 and n using a constant source of randomness

Jelly, 7 6 bytes ⁴!!X%‘ Thanks to @JonathanAllan for golfing off 1 byte! Cannot be run on TIO because (16!)! is a huge number. How it works ...
Dennis's user avatar
  • 210k
16 votes

Plot the Gaussian Distribution in 3D

C++, 3477 3344 bytes Byte count does not include the unnecessary newlines. MD XF golfed off 133 bytes. There's no way C++ can compete for this, but I thought it would be fun to write a software ...
Patrick Purcell's user avatar
15 votes

Sample a random non-decreasing sequence

Python, 89 bytes from random import* lambda n,k:[x-i for i,x in enumerate(sorted(sample(range(1,n+k),k)))] Generating an increasing sequence rather than a non-...
xnor's user avatar
  • 145k
15 votes

What an Odd Function

Husk, 11 10 bytes -1 byte thanks to Leo, and a slightly different function This has an odd probability of 1 !uΣz:NCNİ1 Try ...
H.PWiz's user avatar
  • 11.6k
12 votes

Pick a random number between 0 and n using a constant source of randomness

Mathematica, 29 bytes Based on Dennis's Jelly answer. RandomInteger[2*^9!-1]~Mod~#& I wouldn't recommend actually running this. ...
Martin Ender's user avatar
11 votes

Compute the histogram entropy estimation of a string

Python 3.3+, 64 bytes import math lambda s:sum(math.log2(len(s)/s.count(c))for c in s) Got math.log2 from mbomb007's solution.
xnor's user avatar
  • 145k
11 votes
Accepted

Estimate the mean minimum Hamming distance

Python 3, score = big(?) ...
Anders Kaseorg's user avatar
10 votes

Plot the Gaussian Distribution in 3D

Mathematica, 47 bytes Plot3D[E^(-(x^2+y^2)/2/#^2),{x,-6,6},{y,-6,6}]& takes as input σ Input [2] output -2 bytes thanks to LLlAMnYP
ZaMoC's user avatar
  • 24.9k
10 votes

Frequency Distribution of Multiple Dice Rolls

Octave, 38 bytes @(a,b)round(ifft(fft((a:a*b<a+b)).^a)) Try it online! Explanation Adding independent random variables corresponds to convolving their ...
Luis Mendo's user avatar
  • 105k
10 votes

Calculate the probability of getting to the target first (exactly)

Wolfram Language (Mathematica), 25 bytes $$n! \div \left( \sum ^n _{k=0} k!(n-k)! \right)$$ #!/Sum[k!(#-k)!,{k,0,#}]& Try it online! -4 bytes from @ovs
ZaMoC's user avatar
  • 24.9k
10 votes
Accepted

Weighted coin flip strings

05AB1E, 40 39 18 17 bytes Brute-force approach: 0Lα1ݲãʒγOà³@}èPO Byte-count more than halved by porting @JonathanAllan's Jelly answer, which uses a similar ...
Kevin Cruijssen's user avatar
9 votes

Probability of something happening at least n out of m times

Mathematica, 29 bytes BetaRegularized[#3,#,1+#2-#]& Takes input in the order n,m,p. Mathematica is so good, it even golfs ...
Sp3000's user avatar
  • 61.9k
9 votes

Pick a random number between 0 and n using a constant source of randomness

Brachylog, 9 bytes ≥.∧13ḟṙ|↰ Try it online! This uses 13! like in Martin Ender's answer (13ḟ...
Fatalize's user avatar
  • 38.9k
9 votes

Hand patterns in a card game

APL (Dyalog Unicode), 30 chars ×/!⍨,z,1÷((z←!∘≢⊢)⌸⊢),×∘≢!⍨1⊥⊢ Try it online! Using @orlp’s formula.
FrownyFrog's user avatar
  • 3,732
9 votes

Probabilistic approach puzzle

Wolfram Language (Mathematica), 33 bytes Uses Binomial and outputs ,really fast, fractions Tr@Binomial[#,Prime@Range@#]/2^#& Try it online! The ...
ZaMoC's user avatar
  • 24.9k
9 votes

Probabilistically fair prize splitting for two-player coin toss

R, 36 32 bytes function(X,A,B)pbeta(.5,X-A,X-B) Try it online! The closed formula from this answer suggested a natural probability distribution to use -- this ...
Giuseppe's user avatar
  • 28.6k
9 votes

Lucky dice rolls

AnyDice, 82 bytes function:l N K L{ifL<0{result:[lowestNof(N-L)dK]}else{result:[highestNof(N+L)dK]}} Try it online! For the output, check the "export" view ...
Olivier Grégoire's user avatar
9 votes

Odds for second smallest prime factor

Vyxal, 8 bytes ¡ƛǏḣh;=ṁ Try it Online! -2 thanks to tsh, -1 thanks to lyxal. ...
emanresu A's user avatar
  • 38.8k
8 votes

Probability of All Combinations of Given Events

Haskell, 86 bytes unlines.map(\p->show(fst<$>p)++" = "++show(product$snd<$>p)).mapM(\x->[(0,1-x),(1,x)]) Usage example: ...
nimi's user avatar
  • 35.9k
8 votes
Accepted

Code golf a random orthogonal matrix

Haskell, 169 150 148 141 132 131 bytes ...
Angs's user avatar
  • 4,987
8 votes

A lean, mean bean machine

Python 3, 431 429 410 bytes ...
8 votes

What an Odd Function

Haskell, 35 34 32 bytes f n=n:2*n+1:2*n+3:f(n+2) (f 0!!) Implements the example sequence ...
nimi's user avatar
  • 35.9k
8 votes

What an Odd Function

Everybody does Challenge 1, so let's do the other two. Perl 6, 26 bytes — Challenge 2 {($_==1)+$_-(-1)**($_%%2)} Try it online! It's just ...
Ramillies's user avatar
  • 1,985
8 votes

What an Odd Function

Husk, 8 bytes !uΣzeİ1N Try it online! This implements the example sequence (1,3,2,5,7,4...). Explanation ...
Leo's user avatar
  • 12.7k
8 votes

Frequency Distribution of Multiple Dice Rolls

Mathematica, 29 bytes Tally[Tr/@Range@#2~Tuples~#]& Just generates all possible dice rolls, takes their totals, then counts. Each frequency comes labeled ...
Misha Lavrov's user avatar
  • 5,245
8 votes

Pick-flatten a list

Wolfram Language (Mathematica), 41 20 bytes Flatten@*Tuples//@#& Try it online! Ignore the many warnings, it all works out in the end. How it works For a ...
Misha Lavrov's user avatar
  • 5,245
8 votes

Hand patterns in a card game

Python 3, 134 bytes b=lambda n,k:k<1or n*b(n-1,k-1)/k f=lambda R,D,i=1,s=1,t=0:D and b(R,D[0])*i/s*f(R,D[1:],i+1,(D[0]in D[1:])*s+1,t+D[0])or 1/b(~-i*R,t) ...
orlp's user avatar
  • 39.1k
8 votes

Watch the bookie win

JavaScript (Node.js), 548 516 511 485 453 397 396 392 372 325 323 260 bytes ...
Mayube's user avatar
  • 11.6k

Only top scored, non community-wiki answers of a minimum length are eligible