How do I shuffle a Javascript Array ensuring each Index is in a new position in the new Array?

Richard Bennett

I have an Array of Objects, like so.

var usersGoing = [
    { user: 0 },
    { user: 1 },
    { user: 2 },
    { user: 3 },
    { user: 4 }
];

I need to shuffle this Array so that no Object remains in the same index as when it was instantiated, like so:

[
    { user: 3 },
    { user: 2 },
    { user: 4 },
    { user: 0 },
    { user: 1 }
]

It is IMPERATIVE that the resulting array be sorted in this manner, as each of these user Objects will be assigned to a different user Object.

I have tried a few different sorting algorithms, including Fisher-Yates, and I've tried using Underscore.js' _.shuffle(), and this variant from Kirupa Shuffling an Array in JavaScript:

function shuffleFY(input) {
    for (var i = input.length-1; i >=0; i--) {
        var randomIndex = Math.floor(Math.random()*(i+1)); 
        var itemAtIndex = input[randomIndex]; 

        input[randomIndex] = input[i]; 
        input[i] = itemAtIndex;
    }
    return input;
}

Nothing I've tried is working. Help?

UPDATED: I've marked an answer as correct below, as the key points of the Sattolo Cycle were followed correctly. Also, this is NOT a duplicate of Shuffles Random Numbers with no repetition in Javascript/PHP as this question has the additional requirement of the resulting array not only not containing duplicates, but also cannot contain items in their same initial index position.

Shashank

You posted a link with Sattolo's algorithm in 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

Here it is translated to JavaScript:

function sattoloCycle(items) {
  for(var i = items.length; i-- > 1; ) {
    var j = Math.floor(Math.random() * i);
    var tmp = items[i];
    items[i] = items[j];
    items[j] = tmp;
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to pick a new index of an array by each click

From Dev

How do I extract an element from an array in javascript and then make it an entirely new, single-element array?new

From Dev

How do I add a new attribute to each ActiveRecord in array based on value from another array?

From Dev

How do I assign a new array to each key value in another array

From Dev

How do I shuffle multiple arrays using the same new indexes for each?

From Java

How do I shuffle an array in Swift?

From Dev

How do I shuffle the order of an array in Jekyll?

From Java

How can I add new array elements at the beginning of an array in Javascript?

From Dev

JavaScript : How do i push new column/row to the JavaScript Array during for..loop iteration?

From Dev

How do I agree a new element on a array after do a query?

From Dev

How to rearrange item of an array to new position in Swift?

From Dev

using each_with_index and adding values with corresponding index for a new array

From Dev

New lines in array index

From Dev

How do I update dom-repeat list with new array

From Dev

How do I make a new array nested sorted maps?

From Dev

How do I create a new balanced data frame from an imbalanced data one ensuring a random selection of records?

From Dev

Combine the sub-elements of each array into a new final array - JavaScript

From Dev

Combine the sub-elements of each array into a new final array - JavaScript

From Java

How to randomize (shuffle) a JavaScript array?

From Java

How can I shuffle an array?

From Dev

Global array list, jump to new index each time it's called

From Dev

Shuffle array except for even index's javascript

From Dev

how do i shuffle an array of numbers using python. A number is only allowed to move a step from its origin position

From Dev

How do I create an array in Javascript with different values in each variable?

From Java

How do I check in JavaScript if a value exists at a certain array index?

From Dev

How do I index through a PHP array in Javascript using AJAX?

From Dev

Javascript New Array Functionality

From Dev

Add new array values to existing array position

From Dev

how to shuffle a php array but keep the index in foreach

Related Related

  1. 1

    how to pick a new index of an array by each click

  2. 2

    How do I extract an element from an array in javascript and then make it an entirely new, single-element array?new

  3. 3

    How do I add a new attribute to each ActiveRecord in array based on value from another array?

  4. 4

    How do I assign a new array to each key value in another array

  5. 5

    How do I shuffle multiple arrays using the same new indexes for each?

  6. 6

    How do I shuffle an array in Swift?

  7. 7

    How do I shuffle the order of an array in Jekyll?

  8. 8

    How can I add new array elements at the beginning of an array in Javascript?

  9. 9

    JavaScript : How do i push new column/row to the JavaScript Array during for..loop iteration?

  10. 10

    How do I agree a new element on a array after do a query?

  11. 11

    How to rearrange item of an array to new position in Swift?

  12. 12

    using each_with_index and adding values with corresponding index for a new array

  13. 13

    New lines in array index

  14. 14

    How do I update dom-repeat list with new array

  15. 15

    How do I make a new array nested sorted maps?

  16. 16

    How do I create a new balanced data frame from an imbalanced data one ensuring a random selection of records?

  17. 17

    Combine the sub-elements of each array into a new final array - JavaScript

  18. 18

    Combine the sub-elements of each array into a new final array - JavaScript

  19. 19

    How to randomize (shuffle) a JavaScript array?

  20. 20

    How can I shuffle an array?

  21. 21

    Global array list, jump to new index each time it's called

  22. 22

    Shuffle array except for even index's javascript

  23. 23

    how do i shuffle an array of numbers using python. A number is only allowed to move a step from its origin position

  24. 24

    How do I create an array in Javascript with different values in each variable?

  25. 25

    How do I check in JavaScript if a value exists at a certain array index?

  26. 26

    How do I index through a PHP array in Javascript using AJAX?

  27. 27

    Javascript New Array Functionality

  28. 28

    Add new array values to existing array position

  29. 29

    how to shuffle a php array but keep the index in foreach

HotTag

Archive