[Question]
  [
Because there are not enough simple [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") 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.
  
[Answer]
# MediaWiki templates with [ParserFunctions](https://www.mediawiki.org/wiki/Extension:ParserFunctions), 48 bytes
```
{{#ifexpr:1>{{#time:U}} mod {{{n}}}|true|false}}
```
[Answer]
# Pyth, 3 bytes
```
!OQ
```
[Try it online](http://pyth.herokuapp.com/?code=%21OQ&input=5&debug=0)
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.
[Answer]
## CJam, 5 bytes
Gotta be quick with these ones...
```
rimr!
```
[Test it here.](http://cjam.aditsu.net/#code=rimr!&input=5)
### 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.
```
[Answer]
## TI-BASIC, 4 bytes using [one byte tokens](http://tibasicdev.wikidot.com/one-byte-tokens/ "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.
[Answer]
## 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.
[Answer]
# JavaScript ES6, 15 bytes
-5 bytes thanks to Downgoat.
```
x=>1>new Date%x
```
Based off (uses) of [this](https://codegolf.stackexchange.com/a/66940/31957) answer's technique.
[Answer]
# 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!
[Answer]
# [Microscript II](https://github.com/SuperJedi224/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.
[Answer]
# [Candy](https://github.com/dale6john/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
```
[Answer]
## Seriously, 3 bytes
```
,JY
```
`0` is falsey and `1` is truthy. [Try it online](http://seriouslylang.herokuapp.com/link/code=2c4a59&input=4)
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  
```
[Answer]
# 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.
[Answer]
# Japt, 6 bytes
```
1>U*Mr
```
[Try it online!](http://ethproductions.github.io/japt?v=master&code=MT5VKk1y&input=NA==)
`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.
[Answer]
# [Marbelous](https://github.com/marbelous-lang/docs/blob/master/spec-draft.md), 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.
[Answer]
# 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!
[Answer]
# [PlatyPar](https://github.com/cyoce/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](https://rawgit.com/cyoce/PlatyPar/master/page.html?code=%7E%21)!
[Answer]
# Java, 43 bytes
```
boolean b(int a){return a*Math.random()<1;}
```
[Answer]
# C, 24 bytes
```
f(n){return!(rand()%n);}
```
[Answer]
# ><>, 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>
    ^ ^ ^ ^ ^
```
[Answer]
## 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.
[Answer]
# Python, 42 bytes
```
import random
lambda n:1>random.random()*n
```
*Edit*: Removed the `time.time()` answer because of the distribution.
[Answer]
## [TeaScript](https://esolangs.org/wiki/TeaScript), 3 bytes
```
!N×
```
[Try it here.](http://vihanserver.tk/p/TeaScript/#?code=%22!N%C3%97%22&inputs=%5B%224%22%5D&opts=%7B%22int%22:false,%22ar%22:false,%22debug%22:false,%22chars%22:false,%22html%22:false%7D)
### 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
```
[Answer]
# 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)
```
[Answer]
# Ruby, 14
Function
```
->n{rand(n)<1}
```
Program is slightly longer
```
p rand(gets.to_i)<1
```
[Answer]
# [Perl 6](http://perl6.org), ~~10~~ 8 bytes
```
!(^*).pick
#  ^- The * is the argument
```
This code creates a Range from 0 up-to but excluding the input `*`. It then `pick`s 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
```
[Answer]
# 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
```
[Answer]
## PowerShell, 25 Bytes
```
!(Random -ma($args[0]--))
```
The [`Get-Random` function](https://technet.microsoft.com/en-us/library/hh849905.aspx) when given a `-Ma`ximum 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/n`th 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`.
[Answer]
## 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.
[Answer]
## [Minkolang 0.14](https://github.com/elendiastarman/Minkolang), 7 bytes
```
1nH1=N.
```
[Try it here.](http://play.starmaninnovations.com/minkolang/?code=%24l%5B14H1%3D%5D%24%2BN%2E&input=4)
### 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.
```
[Answer]
# 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`).
[Answer]
## 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;}
```
]