-5
Your task is to generate a random digit following these steps:
First, generate 10 random digits to make a binary number, 50% 0, and 50% 1:
010011001
If it is all zeros, generate a new one.
Convert this to decimal:
153
For each digit, write that amount of alternating 0s and 1s (first digit = 1s, second digit = 0s, third digit = 1s, etc.):
100000111
Convert this to decimal:
263
Take the middle digit (If there is an even number of digits, take the left middle)
6
Remember, this is code-golf, so the code with the smallest number of bytes wins.
1Do we have to use this algorithm, or does it suffice if the distribution is the same? – PurkkaKoodari – 2016-10-22T00:30:40.580
@Pietu1998 If you can prove that the distribution is the same, then OK. – Oliver Ni – 2016-10-22T00:39:19.540
BTW, your example for 10 digits has 9 digits – PurkkaKoodari – 2016-10-22T01:01:27.063
This doesn't actually define a distribution. How should the initial number be generated? – Dennis – 2016-10-22T01:31:49.113
OK, my previous comment might be due to a misunderstanding. Does the generated number have to consist of five 0‘s and five 1's or should 0‘s and 1's follow a u iform distribution? – Dennis – 2016-10-22T01:39:26.503
Hmm good point @Dennis, reading between the lines I think the latter, since otherwise we never get to "If it is all zeros". – Jonathan Allan – 2016-10-22T01:42:24.757
1What do you mean by the second "convert this to decimal" - isn't
100000111
converted to decimal263
? – Jonathan Allan – 2016-10-22T02:15:56.3206
I don't know why I even bother anymore, because you clearly don't listen... Use the Sandbox!
– Mego – 2016-10-22T07:48:45.900Can we assume our random generator is uniform? – Erik the Outgolfer – 2016-10-22T11:13:00.850