29

Dyalog APL, 10 bytes (5?69),?26 Dyadic ? is ⍺ distinct random numbers in [1,⍵], and monadic ? is a single random number. Try it here.


27

MediaWiki templates with ParserFunctions, 48 bytes {{#ifexpr:1>{{#time:U}} mod {{{n}}}|true|false}}


26

x86 machines with rdrand instruction, 10 bytes BITS 64 _try_again: rdrand eax jnc _try_again cmp eax, edi ja _try_again ret machine code 0FC7F0 73FB 39F8 77F7 C3 The input is in the register rdi and the output in rax. This respects the SYS V AMD64 ABI so the code effectively implement a C function unsigned int foo(unsigned int max); with 32-...


24

Python A closed-form formula of p(n) is An exponential generating function of p(n) is where I_0(x) is the modified Bessel function of the first kind. Edit on 2015-06-11: - updated the Python code. Edit on 2015-06-13: - added a proof of the above formula. - fixed the time_limit. - added a PARI/GP code. Python def solve(): # straightforward ...


21

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 ⁴!!X%‘ Main link. Argument: n ⁴ Set the return value to 16. !! Compute (16!)!. X Pseudo-randomly choose an integer between 1 and (16!)!. Since (16!)! is evenly divisible by any k ≤ 2**30, ...


15

Ruby 140 ->s{r=[1.0] s.lines.map{|l|n=[i=0.0]*(r.size+1) l.scan(/\S/).map{|e|a,b=e>?/?e>?]?[0.5]*2:[0,1]:[1,0] z=r[i] n[i]+=z*a n[i+=1]+=z*b} r=n} r} Function that takes as input the string (can be nicely formatted as a pyramid) and returns an array of floats. Test it online: http://ideone.com/kmsZMe Pretty straightforward implementation. Here ...


15

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 without $ because Pyth functions are automatically memoized.


15

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 renderer for the challenge. I tore out and golfed some chunks of GLM for the 3D math and used Xiaolin Wu's line algorithm for rasterization. The program outputs ...


14

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-decreasing one would be straightforward: this is just a random subset of k numbers between 1 and n, sorted. But, we can convert an increasing sequence to a non-decreasing one by shrinking each gap ...


13

Python 2, 269 Here's a nice little expression that evaluates to a function. It accepts three lists of integers. Passes all test cases. lambda A,B,C,w=lambda A,B:cmp(sum(cmp(a,b)for a in A for b in B),0),x=lambda A,B:cmp(sum(cmp(a+c,b+d)for a in A for b in B for c in A for d in B),0): (w(A,B)==w(B,C)==w(C,A)!=0)*((x(A,B)==x(B,C)==x(C,A))*["","strongly ","...


13

Ruby, 140 158 bytes Don't keep upvoting this when there's a better ruby version. Here are more tricks for you. Unnamed function with one argument. Must not contain any spaces. May or may not contain a trailing newline. ->s{Z=(s.split' ')<<[] K=[] F=->i,j,f{k=Z[i][j] K[i]||=0 k==?^?2.times{|m|F[i+1,j+m,f/2]}:!k ?K[j]+=f :F[i+1,j+(k==?/?0:1),f]} F[...


13

CJam, 16 bytes 69,mr5<26mr+:)S* 69, range(69) mr shuffle the generated array 5< first 5 elements 26mr random number 0..25 + concat to array :) increment each array element S* join with spaces Try it online.


12

Pyth, 43 42 41 bytes umsdc+0sm@c[ZJhkJZKcJ2K)2x"\/"ekC,GH2.z]1 This expects the input to be without spaces. Try it online: Pyth Compiler/Executor Pyth, 40 bytes (questionable) umsdc+0sm,K@[ZJhkcJ2)x"\/"ek-JKC,GH2.z]1 Thanks to @isaacg, for saving one byte. Notice that this version didn't actually work in the version of Pyth, when the question was asked. ...


12

CJam, 14 bytes E,_T9t\]ze~es= Test it here. Explanation E, e# Push [0 1 2 ... 12 13]. _ e# Make a copy. T9t\ e# Set the first element to 9. Swap with the original range. ]z e# Wrap them in an array and transpose to get [[9 0] [1 1] [2 2] ... [13 13]. e~ e# Run-length decode to get `[0 0 0 0 0 0 0 0 0 1 2 2 3 3 3 ... 13 13 ... 13 13]. es= e# Use ...


12

CJam, 5 bytes Gotta be quick with these ones... rimr! Test it here. Explanation ri e# Read input and convert to integer N. mr e# Get a uniformly random value in [0 1 ... N-1]. ! e# Logical NOT, turns 0 into 1 and everything else into 0.


12

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. Ansrand<1 also works.


12

Mathematica, 29 bytes Based on Dennis's Jelly answer. RandomInteger[2*^9!-1]~Mod~#& I wouldn't recommend actually running this. 2e9! is a pretty big number... It turns out to be shortest to generate a huge number that is divisible by all possible inputs and the map the result to the required range with a simple modulo. Rejection Sampling, 34 bytes ...


11

Ruby, 204 191 172 characters c,*r=gets.split o=[0]*8 s=->x,y,p{y>14?o[x]+=p :(r.index("#{x},#{y+=1}")?(x<1?s[x+1,y,p]:(x>6?s[x-1,y,p]:(s[x-1,y,p*0.55]+s[x+1,y,p*0.45]))):s[x,y,p])} s[4,0,1] p o[c.to_i] It recursively simulates all possibly outcomes while keeping track of each individual outcome's probability, then it adds that probability to a ...


11

MATL, 5 bytes Three different versions of this one, all length 5. iYr1= which takes an input (i), generates a random integer between 1 and that number (Yr), and sees if it it is equal to 1 (1=). Alternatively, li/r> make a 1 (l, a workaround because there is a bug with doing 1i at the moment), take an input (i), divide to get 1/N (/), make a random ...


11

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

Python 3, score = big(?) from math import exp, log, log1p def f(b, n): e = n * (n - 1) / 2 m = 0 c = 1 s = 0 t = 1 << b for k in range(b): s += c m += exp(e * (log1p(-s / t) if 2 * s < t else log((t - s) / t))) c = c * (b - k) // (k + 1) return m Try it online! The Hamming distance \$D_{x, y}\$ ...


10

CJam, 50 48 45 44 42 40 bytes 1]q{iD%"(+0 0+( (\Y/::+ (d2/_a+"S/=~+}/p This expects the input to be without space and have a trailing newline. For example: ^ \^ ^^\ \^/^ [0 0.1875 0.5625 0.125 0.125] Algorithm The basic idea is that you keep on parsing each character (there are only 4 different characters) and perform different operations on the ...


10

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 whether it's less than 2. There will be a 1/n chance of this happening, and thus a 1/n chance of returning true. Saved 1 byte thanks to Thomas Kwa!


10

JavaScript ES6, 15 bytes -5 bytes thanks to Downgoat. x=>1>new Date%x Based off (uses) of this answer's technique.


9

Python Note: Congratulations to Min_25 for finding a closed-form solution! Thanks for the interesting problem! It can be solved using DP, although I am not currently feeling very motivated to optimise for speed to get a higher score. It could be nice for golf. The code reached N=39 within 10 seconds on this old laptop running Python 2.7.5. from time ...


9

Microscript II, 3 bytes NR! Reads an integer n, generates a random integer between 0 and n-1 (inclusive), then applies a boolean negation to that value.


9

MATL, 10 bytes 69 5Zr26Yr Uses current version (9.2.0) of the language/compiler. Example With compiler run on Matlab: >> matl > 69 5Zr26Yr > 66 59 64 56 29 12 With compiler run on Octave: >> matl > 69 5Zr26Yr > 2 69 41 44 23 22 The first five numbers are separated by space, not by newline. This is due to Octave's underlying ...


9

Mathematica, 29 bytes BetaRegularized[#3,#,1+#2-#]& Takes input in the order n,m,p. Mathematica is so good, it even golfs your code for you: BetaRegularized is the regularised incomplete beta function.


9

Brachylog, 9 bytes ≥.∧13ḟṙ|↰ Try it online! This uses 13! like in Martin Ender's answer (13ḟ is one byte less than 2^₃₀). ṙ is implemented using random_between/3, which, when digging its source, uses random_float/0 which is linked to random/1 which uses the Mersenne Twister algorithm which is uniform for our purposes. Explanation ≥. Input ≥ Output ...


9

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


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