Implement Entombed's lookup table
Jelly, 21 19 15 bytes
ị“£ṅ@kṃżF’b3¤BX
Try it online!
-4 bytes after inspiration from @Neil's Charcoal Answer (binary!).
Try all testcases listed out in a grid (each row is one input tested multiple times).
How?
ị“£ṅ@kṃżF’b3¤BX # Main link
“©½B.ọṅc’ # The integer 1719989029560350
b3 # to base 3: [1,1,2,0,0,2,2,1,1,1,1,2,0,0,0,1,1,1,2,0,0,0,0,2,0,1,2,2,0,0,0,1]
# (2 => R; 1 => 1; 0 => 0)
ị ¤ # Index the input into the base 3 list above
# (1-indexed, and 0 gives the last element)
B # convert to binary: 2 => [0,1], 1 => [1], 0 => [0]
X # Pick a random element from that list
19 byte version
(I personally like this one more because it uses special properties of ị
and X
)
Try it online!
Try all testcases.
ị“©½B.ọṅc’b3¤Hị1,0X # Main link
“©½B.ọṅc’ # The integer 1719989029560350
b3 # to base 3: [2,2,1,0,0,1,1,2,2,2,2,1,0,0,0,2,2,2,1,0,0,0,0,1,0,2,1,1,0,0,0,2]
# (2 => 1; 1 => R; 0 => 0)
ị ¤ # Index the input into the base 3 list above
# (1-indexed, and 0 gives the last element)
H # Halve: [2,1,0] => [1,0.5,0]
ị1,0 # Index into 1,0 (again 1-indexed)
# 1 gives 1, and 0 gives 0
# 0.5 gives [0,1]; since it is a fractional index, it gives both the element corresponding to floor(0.5) and ceil(0.5)
X # Random; 3 different functions
# 0 => 0
# 1 => random integer from 1 to 1 => 1
# [0,1] => random element of [0,1]
JavaScript (ES6), 47 bytes
Expects an integer in \$[0..31]\$ as input.
Similar to @histocrat's Ruby answer, except that the \$\text{R}\$-mask is left-shifted by 1 position so that we can directly get \$0\$ or \$2\$.
n=>Math.random()*(975060894>>n&2)|67571463>>n&1
Try it online!
JavaScript (ES6), 58 56 49 bytes
Expects an integer in \$[0..31]\$ as input.
n=>(Math.random(k=n*5%62%46%18)*2|k<11)&253553>>k
Try it online!
How?
The input \$n\$ is turned into an index \$k \in[0..17]\$ with the following formula:
$$\big(((n\times 5)\bmod 62)\bmod 46\big)\bmod 18$$
In addition to reducing the size of the lookup table, it isolates all \$\text{R}\$ values at the end of the table, with an index greater than \$10\$.
As a string, the lookup table looks as follows:
10001110011RR0RRRR
Therefore, we can use a bitmask to determine if the answer is either \$0\$ or something else, and the test \$k<11\$ to decide between \$1\$ and \$\text{R}\$.
n | * 5 | mod 62 | mod 46 | mod 18 | output
----+-----+--------+--------+--------+--------
0 | 0 | 0 | 0 | 0 | 1
1 | 5 | 5 | 5 | 5 | 1
2 | 10 | 10 | 10 | 10 | 1
3 | 15 | 15 | 15 | 15 | R
4 | 20 | 20 | 20 | 2 | 0
5 | 25 | 25 | 25 | 7 | 0
6 | 30 | 30 | 30 | 12 | R
7 | 35 | 35 | 35 | 17 | R
8 | 40 | 40 | 40 | 4 | 1
9 | 45 | 45 | 45 | 9 | 1
10 | 50 | 50 | 4 | 4 | 1
11 | 55 | 55 | 9 | 9 | 1
12 | 60 | 60 | 14 | 14 | R
13 | 65 | 3 | 3 | 3 | 0
14 | 70 | 8 | 8 | 8 | 0
15 | 75 | 13 | 13 | 13 | 0
16 | 80 | 18 | 18 | 0 | 1
17 | 85 | 23 | 23 | 5 | 1
18 | 90 | 28 | 28 | 10 | 1
19 | 95 | 33 | 33 | 15 | R
20 | 100 | 38 | 38 | 2 | 0
21 | 105 | 43 | 43 | 7 | 0
22 | 110 | 48 | 2 | 2 | 0
23 | 115 | 53 | 7 | 7 | 0
24 | 120 | 58 | 12 | 12 | R
25 | 125 | 1 | 1 | 1 | 0
26 | 130 | 6 | 6 | 6 | 1
27 | 135 | 11 | 11 | 11 | R
28 | 140 | 16 | 16 | 16 | R
29 | 145 | 21 | 21 | 3 | 0
30 | 150 | 26 | 26 | 8 | 0
31 | 155 | 31 | 31 | 13 | 0
Ruby, 35 bytes
->i{[67571463,487530447].sample[i]}
Try it online!
There are 128 different pairs of numbers such that the nth bit is 0 for both when the table's value is 0, 1 for both when the table's value is 1, and different when the table's value is R. So we just choose one of the two at random and take the nth bit.
It seems very likely there's a way to compress this array since we have 128 pairs to choose from, but some quick searching didn't turn it up.