Sattolo cycle

From Rosetta Code
Revision as of 20:09, 29 August 2016 by rosettacode>Aloisdg (Create Sattolo cycle's page. It is use to shuffle an array ensuring each index is in a new position)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Sattolo cycle
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Implement the   Sattolo cycle   for an integer array (or, if possible, an array of any type).

Sattolo cycle is use to shuffle an array ensuring each index is in a new position.

Python

<lang python>from random import randrange

def sattoloCycle(items):

   i = len(items)
   while i > 1:
       i = i - 1
       j = randrange(i)  # 0 <= j <= i-1
       items[j], items[i] = items[i], items[j]
   return