Generate a random digit!

-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 , so the code with the smallest number of bytes wins.

Oliver Ni

Posted 2016-10-22T00:18:24.717

Reputation: 9 650

Question was closed 2016-10-22T17:36:44.933

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 decimal 263? – Jonathan Allan – 2016-10-22T02:15:56.320

6

I don't know why I even bother anymore, because you clearly don't listen... Use the Sandbox!

– Mego – 2016-10-22T07:48:45.900

Can we assume our random generator is uniform? – Erik the Outgolfer – 2016-10-22T11:13:00.850

Answers

2

Pyth, 23 22 bytes

1 byte thanks to @Jakube (swap r operands).

ehc2`ir9,R=!ZjOS1023T2

Try it online.

First, generate 10 random digits to make a binary number, 50% 0, and 50% 1.
If it is all zeros, generate a new one.
Convert this to decimal.

Generate a number between 1 and 1023. OS1023 in Pyth. Then get its digits: jT.

For each digit, write that amount of alternating 0s and 1s (first digit = 1s, second digit = 0s, third digit = 1s, etc.).

Pair each digit with an alternating True or False: ,R=!Z. The alternating booleans come from =!Z, or Z = not Z where Z starts as 0. Then run-length decode: r9.

Convert this to decimal.

Parse integer as binary: i2.

Take the middle digit (If there is an even number of digits, take the left middle).

Take the string representation: `. Split it in two, with the possible middle character going to the left: c2. Take the first half's last character: eh.

PurkkaKoodari

Posted 2016-10-22T00:18:24.717

Reputation: 16 699

r9... instead of r...9 – Jakube – 2016-10-22T10:12:05.810

2

Perl, 62 61 55 53 bytes

Get the digit distribution through a lookup table. Implementing the original algorithm is about 15 bytes longer.

perl -E 'say-(map+(--$n)x$_,unpack"W*","\x90leJZaEC_")[rand 1023]'

Just the code:

say-(map+(--$n)x$_,unpack"W*","\x90leJZaEC_")[rand 1023]

Works as shown,, but replace \x90 by the literal byte to get the claimed score

Ton Hospel

Posted 2016-10-22T00:18:24.717

Reputation: 14 114

2

05AB1E, 19 bytes

To<L.RvTNèy×}JC2䬤

Try it online!

Explanation

153 used as example

To<L                  # [1 ... 2^10-1]
                      # STACK: [1 ... 1023]
    .R                # take random number from range
                      # STACK: 153                          
      v     }         # for each digit
       TNè            # use the digits index in the number to index into 10 
          y×          # repeat the number that many times 
                      # STACK: 1,00000,111
             J        # join to string
                      # STACK: 100000111
              C       # convert to decimal
                      # STACK: 263
               2ä     # split in 2
                      # STACK: [26,3]
                 ¬¤   # take the last digits of the first part
                      # OUTPUT: 6

Emigna

Posted 2016-10-22T00:18:24.717

Reputation: 50 798

0

Jelly 17 bytes

⁵Ḷx“ḶƇleJZaEC_‘µX

TryItOnline!

How?

The distribution of the first step are [1,1023] with equal likelihood.
Applying the transformation instructed for each of those numbers produces a distribution of:
{0: 178, 1: 144, 2: 108, 3: 101, 4: 74, 5: 90, 6: 97, 7: 69, 8: 67, 9: 95}

⁵Ḷx“ḶƇleJZaEC_‘µX - Main link: (niladic)
⁵                 - literal 10
 Ḷ                - range -> [0,1,2,3,4,5,6,7,8,9]
   “ḶƇleJZaEC_‘   - code page indexes -> [178,144,108,101,74,90,97,69,67,95]
  x               - repeat - > [178 zeros, 144 ones, 108 twos, ...]
               µ  - monadic chain separation
                X - pick a random element

Jonathan Allan

Posted 2016-10-22T00:18:24.717

Reputation: 67 804