# Generate a random digit! [closed]

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.

## closed as unclear what you're asking by Oliver Ni, Mego♦, Rɪᴋᴇʀ, cat, Peter TaylorOct 22 '16 at 17:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Do we have to use this algorithm, or does it suffice if the distribution is the same? – PurkkaKoodari Oct 22 '16 at 0:30
• @Pietu1998 If you can prove that the distribution is the same, then OK. – Oliver Ni Oct 22 '16 at 0:39
• BTW, your example for 10 digits has 9 digits – PurkkaKoodari Oct 22 '16 at 1:01
• What do you mean by the second "convert this to decimal" - isn't 100000111 converted to decimal 263? – Jonathan Allan Oct 22 '16 at 2:15
• I don't know why I even bother anymore, because you clearly don't listen... Use the Sandbox! – Mego Oct 22 '16 at 7:48

# 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


# Pyth, 23 22 bytes

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

ehc2ir9,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.

• r9... instead of r...9 – Jakube Oct 22 '16 at 10:12

# Perl, 626155 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

# 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, ...]