0

I know how to generate random sequence of list of numbers. Here is an example :

lst = np.arange(10) #lst always 0 .. n
np.shuffle(lst)

the question is how to shuffle the numbers so that the number ONE is NOT at position ONE, 2 is not at pos 2, and in general x is not at position x. For all 0 .. n.

The initial sequence is always 0 .. n, where n will be in hundreds, 100 or 200 or 300.


looked at the thread... the shuffle methods seem unusable, too slow. This is my current experiment, so far seems to work :

def swap(ary,pos):
  new_pos = np.random.randint(pos+1, len(ary))
  tmp = ary[pos]
  ary[pos] = ary[new_pos]
  ary[new_pos] = tmp
  #print "%s <=> %s" % (pos, new_pos)

def derange(n):
  lst = np.arange(n)
  for i in np.arange(n-1) : swap(lst, i)
  return lst 
sten
  • 7,028
  • 9
  • 41
  • 63
  • 1
    Shuffle and check, repeat. If that's not a solution you need to define your environment more as what's important and what's not (performance, bias). – sascha Apr 15 '17 at 19:20
  • What is the purpose? An obvious answer would be to use np.arange(10,20) then shuffle. So why is position and the value key to your problem. – NaN Apr 15 '17 at 19:23

0 Answers0