33
\$\begingroup\$

Because there are not enough simple challenges:

Create an optionally unnamed program or function that, given (by any means) an integer 1 ≤ N ≤ 10000, outputs your language's True value with a pseudo-random probability of 1/N, False otherwise.

Please note that the requirement for naming has been removed. Feel free to edit answers and scores accordingly.

Some languages use 1 (or -1) and 0 for True and False, that is fine too.

Example:

Example input tests:

4 -> True
4 -> False
4 -> False
4 -> False
4 -> False
4 -> True
4 -> False
4 -> False

I.e. given 4; it returns True with a 25% chance and False with a 75% chance.

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Relevant meta post \$\endgroup\$ Commented Dec 17, 2015 at 19:38
  • 1
    \$\begingroup\$ Also relevant meta post. \$\endgroup\$ Commented Dec 17, 2015 at 19:40
  • \$\begingroup\$ As not all languages have built in "pseudorandomness" is it possible to get a seed as second argument? (E.g. Brainfuck) \$\endgroup\$
    – flawr
    Commented Dec 22, 2015 at 21:59
  • \$\begingroup\$ @flawr use current millisecond... \$\endgroup\$
    – Adám
    Commented Dec 23, 2015 at 2:17
  • 1
    \$\begingroup\$ What's the largest N we have to accept? \$\endgroup\$ Commented Apr 12, 2016 at 16:20

49 Answers 49

27
\$\begingroup\$

MediaWiki templates with ParserFunctions, 48 bytes

{{#ifexpr:1>{{#time:U}} mod {{{n}}}|true|false}}
\$\endgroup\$
3
  • 14
    \$\begingroup\$ Interesting choice of language :-) \$\endgroup\$
    – Adám
    Commented Dec 18, 2015 at 3:27
  • 6
    \$\begingroup\$ Who the heck thought it would be sensible to add non-deterministic functions to MediaWiki templates!? \$\endgroup\$ Commented Dec 19, 2015 at 11:05
  • 4
    \$\begingroup\$ @immibis: well the non-determinism occurs from #time, probably to update the age of living people, etc. \$\endgroup\$ Commented Dec 19, 2015 at 21:42
15
\$\begingroup\$

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.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ It is possible. \$\endgroup\$
    – Leaky Nun
    Commented May 22, 2016 at 1:33
  • \$\begingroup\$ @LeakyNun Ah right, I had forgotten about using the time for a random function, that's rather clever. \$\endgroup\$ Commented May 22, 2016 at 1:43
  • \$\begingroup\$ No, I just used time to de-memoize it. \$\endgroup\$
    – Leaky Nun
    Commented May 22, 2016 at 1:47
  • 1
    \$\begingroup\$ I'm aware, I guess I just didn't word it well :P That said I don't think it would really work as a workaround for most submissions, if it were ever better than a full program for some reason. Taking the time as an argument probably isn't just allowed by default. \$\endgroup\$ Commented May 22, 2016 at 6:00
  • 1
    \$\begingroup\$ @LeakyNun I believe this question predates the Q filling in at the end, as otherwise I would have answered !O ;) \$\endgroup\$ Commented Jun 10, 2016 at 12:43
12
\$\begingroup\$

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.
\$\endgroup\$
1
  • 11
    \$\begingroup\$ "Gotta be quick with these ones..." which is a reason to disagree with OP that "there are not enough simple code-golf challenges". If FGITW is an issue, IMO it's too simple. \$\endgroup\$ Commented Dec 17, 2015 at 19:51
12
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ How... Is that four bytes? \$\endgroup\$ Commented Dec 19, 2015 at 7:17
  • 3
    \$\begingroup\$ @JanDvorak The first bytes is not(, the next is int(, the next is Ans, the next is rand. In general, graphing calculators do not use ASCII as their internal representation for programs. \$\endgroup\$ Commented Dec 19, 2015 at 11:06
  • \$\begingroup\$ @immibis In general, computers don't use ASCII either. This might be questionable, is there a meta discussion about that? \$\endgroup\$
    – Kroltan
    Commented Dec 19, 2015 at 15:45
  • 7
    \$\begingroup\$ @Kroltan Yes; this is the meta discussion, and this is the list of tokens that are one byte, which includes all four that I used. \$\endgroup\$
    – lirtosiast
    Commented Dec 19, 2015 at 16:33
  • \$\begingroup\$ @ThomasKwa thanks! \$\endgroup\$
    – Kroltan
    Commented Dec 19, 2015 at 16:40
11
\$\begingroup\$

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 number between 0 and 1 (r), and see if the random number is smaller than 1/N. Or,

ir*1<

take and input (i), and multiply by a random number between 0 and 1 (r*), and see if the result is smaller than 1 (1<).

In Matlab, not MATL, you can do this anonymous function

@(n)n*rand<1

for 12 bytes, which is used by doing ans(5), for example.

\$\endgroup\$
10
\$\begingroup\$

JavaScript ES6, 15 bytes

-5 bytes thanks to Downgoat.

x=>1>new Date%x

Based off (uses) of this answer's technique.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ new Date can also work and might save a few bytes \$\endgroup\$
    – Downgoat
    Commented Dec 17, 2015 at 22:59
  • \$\begingroup\$ @Downgoat Ah, right, Date randomness! \$\endgroup\$ Commented Dec 17, 2015 at 23:03
10
\$\begingroup\$

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!

\$\endgroup\$
0
9
\$\begingroup\$

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.

\$\endgroup\$
0
8
\$\begingroup\$

Candy, 2 bytes

Hn

H stands for Heisen-double

n stands for not

The 'n' is passed with the -i flag as numeric input. Values left on the stack are printed on exit.

"Long" form:

rand   # number between 0 and pop()
not    # cast to int, invert non-zero to zero, and zero to one
\$\endgroup\$
5
  • \$\begingroup\$ I think you'd need to count -i as one byte. \$\endgroup\$
    – lirtosiast
    Commented Dec 19, 2015 at 5:54
  • 1
    \$\begingroup\$ basically the only way to pass numeric input is with the -i flag. I guess only languages that read from stdin suffer no input specification penalty? \$\endgroup\$ Commented Dec 19, 2015 at 6:04
  • \$\begingroup\$ Well, if there were a generic input flag, or you just used regular CLAs to pass arguments, that would definitely be fine. It seems unfair that the datatype is being specified for free, though. \$\endgroup\$
    – lirtosiast
    Commented Dec 19, 2015 at 6:11
  • 2
    \$\begingroup\$ @ThomasKwa Should a function written in a dynamic language have to count the bytes to specify that the argument is an integer in the documentation? In lambda x: random.random()<1/x (ungolfed) it's also "specified for free" that the argument is a number. \$\endgroup\$ Commented Dec 19, 2015 at 11:11
  • \$\begingroup\$ @immibis Hmm, that's a good point. I guess trying to keep the rules for programs and functions the same should allow this, then. I'll make a post on meta. \$\endgroup\$
    – lirtosiast
    Commented Dec 19, 2015 at 16:31
7
\$\begingroup\$

Seriously, 3 bytes

,JY

0 is falsey and 1 is truthy. Try it online

Explanation:

,JY
,    get input
 J   push a random integer in range(0, input) ([0, ..., input-1])
  Y  logical not: push 0 if truthy else 1  
\$\endgroup\$
7
\$\begingroup\$

R, 30 22 bytes

code

cat(runif(1)<1/scan())          #new
f=function(N)cat(runif(1)<1/N)  #old

It generates a number from a uniform distribution (0 to 1) and should evaluate to true 1/n of the times.

\$\endgroup\$
0
6
\$\begingroup\$

Japt, 6 bytes

1>U*Mr

Try it online!

Mr is equivalent to JS's Math.random. The rest is pretty obvious. I could probably add a number function that generates a random float between 0 and the number. When this happens, two bytes will be saved:

1>Ur    // Doesn't currently work

Alternate version:

1>Ð %U

Ð is equivalent to new Date(, and the Date object, when asked to convert to a number, becomes the current timestamp in milliseconds. Thus, this is entirely random, unless it is run multiple times per ms.

\$\endgroup\$
6
\$\begingroup\$

Marbelous, 21 bytes

}0    # takes one input n
--    # decrements n
??    # random value from range 0..n (inclusive)
=0?0  # push right if not equal to 0, fall through otherwise | convert to zero
++    # increment | no-op
{0//  # output | push left

I've taken 0 to be falsey and 1 to be truthy, though there is no real reason for that seeing as Marbelous doesn't really have an if. More Marbelousy would be output to {0 for true and {> for false. This would look like this:

}0
--
??
=0{>
{0

But I'm not sure that's valid.

\$\endgroup\$
3
  • \$\begingroup\$ I'd be up for a meta discussion on this. Short version of my view: outputting a value to a different output is equivalent to having different output tuples in another language. If (nil,1) and (1,nil) can be your truthy and falsey values in another language, then {0 vs {> should be allowed in Marbelous. PS: your {> version won't exit because you never fill the other output. \$\endgroup\$
    – Sparr
    Commented Dec 18, 2015 at 21:17
  • \$\begingroup\$ @Sparr it will exit due to inactivity, no? \$\endgroup\$
    – overactor
    Commented Dec 19, 2015 at 14:08
  • \$\begingroup\$ You're right. I feel dumb. \$\endgroup\$
    – Sparr
    Commented Dec 19, 2015 at 21:32
6
\$\begingroup\$

APL, 6 3 bytes

+=?

This is a function train that takes an integer and returns 1 or 0 (APL's true/false). We generate a random integer from 1 to the input using ?, then check whether the input is equal to that integer. That results in a 1/input chance of true.

Saved 3 bytes thanks to Thomas Kwa!

\$\endgroup\$
5
  • \$\begingroup\$ @ThomasKwa I thought about some kind of train, but does that really count as a "named function" if assigned? I guess the "named" part is throwing me here since it's atypical. \$\endgroup\$
    – Alex A.
    Commented Dec 17, 2015 at 22:53
  • \$\begingroup\$ @ThomasKwa Assignment of trains (and derived functions) is completely parallel to all other assignments. \$\endgroup\$
    – Adám
    Commented Dec 18, 2015 at 3:26
  • \$\begingroup\$ @NBZ what do you mean by parallel? \$\endgroup\$
    – lirtosiast
    Commented Dec 18, 2015 at 16:04
  • \$\begingroup\$ @ThomasKwa Equivalent; behaving like any other function assignment. \$\endgroup\$
    – Adám
    Commented Dec 20, 2015 at 16:18
  • \$\begingroup\$ I would use instead of '+' because + means Conjugate for complex numbers. Of course it doesn't matter here, and + is the traditional identity (no-op) function, but now we have (same). Other no-ops for scalars are: (materialize), (pick), (enclose), (split), (mix), (unique), (enlist), , (ravel), (table), (reverse), (reverse first), and (transpose). Some change the scalar into a vector or matrix. \$\endgroup\$
    – Adám
    Commented Dec 31, 2015 at 19:46
6
\$\begingroup\$

PlatyPar, 3 bytes

#?!

#? gets a random number [0,n) where n is input. ! returns true if the number before it is 0, else it returns false.

Using more recent features that were implemented (but unfortunately for me not committed) before this question was asked I can get it down to 2 with ~! Try it online!

\$\endgroup\$
0
5
\$\begingroup\$

Java, 43 bytes

boolean b(int a){return a*Math.random()<1;}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ a->a*Math.random()<1 is shorter. \$\endgroup\$ Commented Dec 17, 2015 at 19:49
  • \$\begingroup\$ Should specify "Java 7 or earlier". \$\endgroup\$
    – corsiKa
    Commented Dec 18, 2015 at 18:39
  • \$\begingroup\$ @corsiKlauseHoHoHo This works in Java 8 as well though \$\endgroup\$ Commented Dec 18, 2015 at 19:07
  • 1
    \$\begingroup\$ Of course it does - but it's not golfed for Java 8, which would use lambdas to save space. By that logic, all Java answers are also Groovy answers, but Groovy is always the same or smaller because it has shortcuts that Java doesn't. \$\endgroup\$
    – corsiKa
    Commented Dec 18, 2015 at 19:51
5
\$\begingroup\$

C, 24 bytes

f(n){return!(rand()%n);}
\$\endgroup\$
2
  • \$\begingroup\$ I've rolled back OP's edit removing the first 4 characters. It's nice to have bytes reduced, but to me, having the returnwithout the f(n) doesn't make any sense syntactically. \$\endgroup\$ Commented Dec 18, 2015 at 3:18
  • 1
    \$\begingroup\$ @insertusernamehere rand()%n is a standard way of getting a random number in the range 0..n-1. You are correct, it does rely on n being much smaller than RAND_MAX but there is no upper limit for n mentioned in the question. An alternative approach would be to do a reject and re-roll on all numbers from n to RAND_MAX but it would be hopelessly inefficient at small n. \$\endgroup\$ Commented Dec 21, 2015 at 0:00
5
\$\begingroup\$

><>, 27 + 3 for -v = 30 bytes

Here is a not-uniform-at-all solution where I mod N the sum of 15876 random picks of 0 or 1 :

0"~":*>:?vr%0=n;
1-$1+$^-1x

N must be input on the stack with -v flag, output is 0 for falsey and 1 for truthy.

A much smarter and uniform solution that work for 1/2^N instead :

4{:?!v1-}:">"$2p:"x"$3p:"^"$4p1+:">"$3p1+!
   ^1<
0n;
1n;>
 

For an input 3 you've got 1/8 chances of getting 1 and 7/8 of getting 0.

Explanation :

I append as much x as needed on the 4th line and surround them with directions so there is only two ways out of the x: either the falsey output or the next x. If all x go in the right direction, the last one will route to the truthy output.

For example for N=5, the final codespace is the following :

4{:?!v1-}:">"$2p:"x"$3p:"^"$4p1+:">"$3p1+!
   ^1<
0n; > > > > >
1n;>x>x>x>x>x>
    ^ ^ ^ ^ ^
\$\endgroup\$
5
  • \$\begingroup\$ While it's true you can't get a perfect distribution for arbitrary N, neither can anyone else using a PRNG. You could loop through an x a few times to get a bunch of random bits, assemble them into an int I, then use I%N as your random value. \$\endgroup\$
    – Sparr
    Commented Dec 18, 2015 at 21:14
  • \$\begingroup\$ @Sparr edited my answer but I feel like using a big number of iteration will 'average out' the resulting integer, making the output heavily tend toward (iterNum/2)%N. I don't think using a lower number would be a solution either. Did I maybe not quite understood you, or would you have any further idea to better the solution? \$\endgroup\$
    – Aaron
    Commented Dec 20, 2015 at 13:32
  • \$\begingroup\$ instead of adding 15000 bits together, produce just 32 bits and concatenate them, giving you a uniformly distributed 32-bit random integer. mod that. \$\endgroup\$
    – Sparr
    Commented Dec 21, 2015 at 1:50
  • \$\begingroup\$ @Sparr that seems better indeed, even if I've no idea why ;) That'll cost much more bytes (><> sucks for byte operations & base conversion) but I'll change my answer this evening (CEST). \$\endgroup\$
    – Aaron
    Commented Dec 21, 2015 at 11:54
  • \$\begingroup\$ you can concatenate bits by multiplying by two and adding: r=0; for(0..32) r=r*2+randbit; \$\endgroup\$
    – Sparr
    Commented Dec 21, 2015 at 18:22
4
\$\begingroup\$

Mathematica, 18 16 bytes

#RandomReal[]<1&

Basic solution. The unnamed Function creates a random number in [0, 1), multiplies it by its argument, and checks if it is still less than 1.

\$\endgroup\$
0
4
\$\begingroup\$

Python, 42 bytes

import random
lambda n:1>random.random()*n

Edit: Removed the time.time() answer because of the distribution.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ For random, it's worth doing from random import* to save on random.. Not for time though. \$\endgroup\$
    – xnor
    Commented Dec 18, 2015 at 5:03
  • 1
    \$\begingroup\$ Random numbers generated by modulo division are not uniformly distributed. \$\endgroup\$
    – Trang Oul
    Commented Dec 18, 2015 at 8:35
  • \$\begingroup\$ @TrangOul That's a good point; for larger n the effect could be noticeable. I think 1>time.time()%1*n could work. \$\endgroup\$
    – lirtosiast
    Commented Dec 18, 2015 at 16:07
  • \$\begingroup\$ @TrangOul I presume you know the difference between rand in C, and time.time in Python... One obvious feature of the latter is that it returns the current time, which is unbounded, so that time.time()%n has a uniform distribution (over long enough periods of time) for any n. \$\endgroup\$ Commented Dec 19, 2015 at 11:09
4
\$\begingroup\$

TeaScript, 3 bytes

!N×

Try it here.

Explanation

 N  maps to Math.rand which is a utility function that returns an integer
    between `arg1` and `arg2` or `0` and `arg1` if only one argument is
    provided.
  × is expanded to `(x)`, where `x` is initialised with the value provided
    in the input boxes; × represents byte '\xd7'
!   negate the result, 0 results in true, anything else false
\$\endgroup\$
2
  • 1
    \$\begingroup\$ How does seven chars add up to 6 bytes? And is the (R) a (single byte) ANSI char? \$\endgroup\$
    – Adám
    Commented Dec 20, 2015 at 12:52
  • \$\begingroup\$ @NBZ, haha! I think I lied... I blame hangover... I'll update now as I'm going to change the mechanism to a more straightforward one that I've just noticed! In this current version the ® represents the char '\xae' so is just one byte. :) \$\endgroup\$ Commented Dec 20, 2015 at 13:12
4
\$\begingroup\$

Fuzzy Octo Guacamole, 10 bytes

^-!_[0]1.|

Explanation:

^-!_[0]1.|

^          # Get input.
 -         # Decrement, so we can append <ToS> zeros and a 1 to the stack.
  !        # Set loop counter.
   _       # Pop, since we are done with the input.
    [      # Start loop
     0     # Push 0
      ]    # End for loop. We have pushed input-1 0s to the stack.
       1   # Push a single 1 to the stack.
        .  # Switch stacks
         | # Pick a random item from the inactive stack, which has n-1 falsy items and 1 truthy item, so the truthy probability is 1/n.
           # (implicit output)
\$\endgroup\$
3
\$\begingroup\$

Ruby, 14

Function

->n{rand(n)<1}

Program is slightly longer

p rand(gets.to_i)<1
\$\endgroup\$
0
3
\$\begingroup\$

Perl 6,  10  8 bytes

!(^*).pick
#  ^- The * is the argument

This code creates a Range from 0 up-to but excluding the input *. It then picks one at random and the ! returns True when it receives a 0.

1>*.rand
# ^- The * is the argument

This takes the input * and multiplies it by a random Num from 0..^1 then returns True if it was smaller than 1.

# store it in a lexical code variable for ease of use
my &code = 1>*.rand;

die "never dies here" unless code 1;

for ^8 { say code 4 }
False
True
False
False
False
True
False
False
\$\endgroup\$
3
\$\begingroup\$

Prolog (SWI), 24 bytes

Code:

p(N):-X is 1/N,maybe(X).

maybe(+P) is a function which succeeds with probability P and fails with probability 1-P

Example:

p(4).
false

p(4).
false

p(4).
true
\$\endgroup\$
3
\$\begingroup\$

PowerShell, 25 Bytes

!(Random -ma($args[0]--))

The Get-Random function when given a -Maximum parameter n returns a value from the range [0,n). We leverage that by subtracting 1 from our input $args[0], so we're properly zero-indexed, and get a random value. Precisely 1/nth of the time, this value will be 0, so when we Boolean-not it with ! it will return True. The other times will return False.

\$\endgroup\$
3
\$\begingroup\$

J, 3 bytes

0=?

This is a monadic fork that takes an argument on the right. Similarly to APL, ? generates a random integer; however, J arrays are zero-based. So we compare to 0 instead of to the input.

\$\endgroup\$
3
\$\begingroup\$

Minkolang 0.14, 7 bytes

1nH1=N.

Try it here.

Explanation

1          Pushes 1
 n         Takes number from input
  H        Pops b,a and pushes a random integer between a and b, inclusive
   1=      1 if equal to 1, 0 otherwise
     N.    Output as number and stop.
\$\endgroup\$
3
\$\begingroup\$

PHP, 22 bytes

<?=2>rand(1,$argv[1]);

Reads n from command line, like:

$ php probability.php 4

Outputs (false is cast to an empty string in PHP) or 1 (in case of true).

\$\endgroup\$
3
\$\begingroup\$

C#, 56 45 bytes

Thanks to, pinkfloydx33 it's 45 now.

bool b(int n){return new Random().Next(n)<1;}

Old 56 bytes

Generates random positive integer bigger or equal to 0 and smaller than n and checks if it's smaller than 1 and return comparison result.

bool a(int n){Random r=new Random();return r.Next(n)<1;}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to PPCG! Unfortunately, this submission doesn't work, because Random.Next(k) returns an integer k such that 0 <= k < n. By changing the condition to <1, it will be correct. In addition, the use of a lambda expression may get your code shorter. \$\endgroup\$
    – user45941
    Commented Dec 18, 2015 at 8:30
  • \$\begingroup\$ @Mego Sure, thank you for comment. I made it 0 < k <= n and it should be like you said. I'll correct it immediately. \$\endgroup\$
    – ivaan
    Commented Dec 18, 2015 at 8:41
  • 2
    \$\begingroup\$ Use var r saves three. Or if c#6, bool a(int n) => new Random().Next(n)<1; for 41. Though not sure if initializing a new Random per method call will work properly as far as distribution? \$\endgroup\$ Commented Dec 18, 2015 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.