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
...
29
votes
Accepted
Choose The Powerball Numbers!
Dyalog APL, 10 bytes
(5?69),?26
Dyadic ? is ⍺ distinct random numbers in [1,⍵], and monadic ...
27
votes
1/N probability
MediaWiki templates with ParserFunctions, 48 bytes
{{#ifexpr:1>{{#time:U}} mod {{{n}}}|true|false}}
26
votes
Accepted
Pick a random number between 0 and n using a constant source of randomness
x86 machines with rdrand instruction, 10 bytes
...
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
...
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 ...
15
votes
1/N probability
Pyth, 3 bytes
!OQ
Try it online
Simple inversion of random choice from 0 to input
Amusingly in Pyth it is not possible to make a function that does this ...
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-...
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 ...
13
votes
12
votes
12
votes
1/N probability
CJam, 5 bytes
Gotta be quick with these ones...
rimr!
Test it here.
Explanation
...
12
votes
1/N probability
TI-BASIC, 4 bytes using one byte tokens
not(int(Ansrand
Determines if the integer part of the input times a random number in [0,1) is zero. ...
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. ...
11
votes
1/N probability
MATL, 5 bytes
Three different versions of this one, all length 5.
iYr1=
which takes an input (i), generates a random integer ...
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.
11
votes
Accepted
10
votes
1/N probability
Julia, 17 16 15 bytes
n->2>rand(1:n)
This is a function that generates a random integer between 1 and n and tests ...
10
votes
1/N probability
JavaScript ES6, 15 bytes
-5 bytes thanks to Downgoat.
x=>1>new Date%x
Based off (uses) of this answer's technique.
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
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 ...
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
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 ...
9
votes
1/N probability
Microscript II, 3 bytes
NR!
Reads an integer n, generates a random integer between 0 and <...
9
votes
Choose The Powerball Numbers!
MATL, 10 bytes
69 5Zr26Yr
Uses current version (9.2.0) of the language/compiler.
Example
With compiler run on Matlab:
...
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 ...
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ḟ...
9
votes
Hand patterns in a card game
APL (Dyalog Unicode), 30 chars
×/!⍨,z,1÷((z←!∘≢⊢)⌸⊢),×∘≢!⍨1⊥⊢
Try it online!
Using @orlp’s formula.
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
probability-theory × 67code-golf × 58
math × 17
random × 9
code-challenge × 6
combinatorics × 6
number × 5
rational-numbers × 4
game × 3
fastest-code × 3
card-games × 3
simulation × 3
array × 2
arithmetic × 2
integer × 2
graph-theory × 2
classification × 2
statistics × 2
string × 1
ascii-art × 1
kolmogorov-complexity × 1
decision-problem × 1
number-theory × 1
graphical-output × 1
matrix × 1