For the sake of simplicity, I am providing a mapping from $\{0 \cdots (n! - 1)\}$ to the set of permutations of $\{0, 1, \cdots, (n-1)\}$. Please offset by 1 where required.
First of all, note that $n! = \sum_{i=1}^{n-1}i.i! + 1$ (can be easily proved by induction).
So for any number k in the range you provided, k - 1 can be written in the form
$k - 1 = \sum_{i=1}^{n-1}a_{i}i!$, where in each case $a_i$ is an integer from $[0, i]$.
Associate with k the permutation $p_{1}p_2p_3\cdots p_{n-1}p_n$ where $p_1$ = $a_1$, $p_2$ is the $a_2$th element in $\{0, 1, \ldots, {p_1-1}, {p_1+1}, \ldots (n-1)\}$, and so on.
You can easily see that a unique permutation is associated with each $k$ here.
EDIT: I went ahead and wrote the code. See this:
#!/usr/bin/python
import sys
def get_perm(n, k):
"""get permutation corressponding to random number. k < n!"""
used = [False] * n
fact_arr = [0] * n
this_fact = 1
perm = []
for i in range(n):
this_fact *= (i + 1)
fact_arr[i] = this_fact
k1 = k
for i in range(n - 1):
this_fact = fact_arr[n - 2 - i]
p = k1 / this_fact
k1 = k1 % this_fact
p1 = p
idx = 0
while p1 > 0 or used[idx]:
if not used[idx]:
p1 -= 1
idx += 1
perm.append(idx + 1)
used[idx] = True
for i in range(n):
if not used[i]:
perm.append(i + 1)
break
return perm
def main():
perm = get_perm(int(sys.argv[1]), int(sys.argv[2]) - 1)
print perm
if __name__ == '__main__':
main()
Now if I run, eg.
for i in \$(seq 1 6); do ./permutations.py 3 \$i; done
The output is:
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]