# [Numpy-discussion] randint for long type (permutations)

Robert Kern robert.kern@gmail....
Fri Jun 15 12:52:22 CDT 2007

```Will Woods wrote:
>
> The range of N I need is from 5-100, which spans the highly likely to
> highly improbable for M around 1000-10000. The permutation can be
> derived from an integer using the algorithm here:
> http://en.wikipedia.org/wiki/Permutation

You really, really don't want to do it this way. 100! is a huge number that you
*cannot* sample effectively. With the permutation algorithm given here, only the
first fraction of the sequence will be shuffled at all.

In [29]: def perm(k, s):
fact = 1
for j in range(2, len(s)+1):
fact *= j-1
i = j - ((k//fact) % j) - 1
tmp = s[i], s[j-1]
s[j-1], s[i] = tmp
return s
....:

In [37]: s0 = range(100)

In [38]: print s0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

In [39]: print perm(10000000000000, s0[:])
[9, 12, 11, 4, 13, 14, 8, 7, 15, 5, 10, 0, 3, 1, 2, 6, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

Please, take my advice and shuffle the sequences on the master node using
numpy.random.permutation() and distribute the sequences among the worker nodes.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
```