1/N probability

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.

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

MediaWiki templates with ParserFunctions, 48 bytes

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

Pyth, 3 bytes

!OQ

Try it online

Simple inversion of random choice from 0 to input

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, 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. 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. 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. PHP, 22 bytes <?=2>rand(1,$argv);

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).

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;}
• 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. – Mego Dec 18 '15 at 8:30
• @Mego Sure, thank you for comment. I made it 0 < k <= n and it should be like you said. I'll correct it immediately. – ivaan Dec 18 '15 at 8:41
• 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? – pinkfloydx33 Dec 18 '15 at 9:56

when gf clicked