30
0
Because there are not enough simple 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.
2Relevant meta post – FryAmTheEggman – 2015-12-17T19:38:28.260
1
Also relevant meta post.
– AdmBorkBork – 2015-12-17T19:40:56.103As not all languages have built in "pseudorandomness" is it possible to get a seed as second argument? (E.g. Brainfuck) – flawr – 2015-12-22T21:59:39.853
@flawr use current millisecond... – Adám – 2015-12-23T02:17:06.330
@NBZ Brainf*** doesn't have that either – SuperJedi224 – 2016-02-12T01:10:26.837
@SuperJedi224 Right. OK then; have a seed and optionally return a new seed. But only for languages that really don't have other options. – Adám – 2016-02-12T14:09:34.510
1What's the largest
N
we have to accept? – Toby Speight – 2016-04-12T16:20:47.277@TobySpeight 10000. – Adám – 2016-06-10T13:16:00.393