Ooops, I think I made some errors :

first replace 20 by 21 to get the ri in the range 0…20

And even then , it does not work : you have duplicates (take tid = 20 and tid = 41, you get 20,0,0,0,0 both times.

The answer is more complex :

let P(i, j) be the number of permutation of i elements in j bins (you are looking for all the P(21, 5) permutations in your example)

let S(k, j) be the sum P(0, j)+P(1,j) +… + P(k, j) (and with S(-1, j) = 0 by convention)

r0 is the number that verifies S(r0-1, 5) <= tid < S(r0, 5)

then let t1 = tid-S(r0-1, 5) , and choose r1 : the number that verifies

S(r1-1, 4) <= t1 < S(r1, 4)

and so on with t2 = t1-S(r1-1, 4) …

I think this should work this time, correct me if I am still wrong.

PS : to fasten your computation, you can precompute all the P(i, j) and S(i, j), you have 5*21 = 105 of them.