-1

In javascript, I am little bit confused that how to get the actual and accurate probability of shuffling an object in an array. For example

var numberOfOrder=[
  {
    id:1 
  },
  {
    id:2 
  },
  {
    id:3 
  }
]

From above example The above object can be manipulated in 6 ways By finding the factorial numberOfOrder.length;

But what is the actual way to shuffle that object in an array.

My Try

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

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

But the above function won't return accurate value if I run that function 6 times it returning Duplicate Values

What is the correct function to do it

Cœur
  • 32,421
  • 21
  • 173
  • 232
Nane
  • 1,361
  • 3
  • 23
  • 61

2 Answers2

1

You have to understand the difference between probability and permutations. The second term comes from combinatorics. There are some algorithms that allow to get all possible permutations of array items. Here is one of them:

function permutations(items) {
    // single item array - no permutations available
    if (items.length == 1) return [items];
    var combos = [];
    for (var i = 0; i < items.length; i++) {
        // first - the current item, rest - array without the current item
     var first = items[i], rest = items.slice(0);
        rest.splice(i, 1);
        // getting permutations of shorter array and for each of them...
        permutations(rest).forEach(function(combo){
            // prepend the current item
            combo.unshift(first);
            // save the permutation
            combos.push(combo);
        });
    }
    return combos;
}

alert(permutations([ 1, 2, 3 ]).join("\n"));

Update

The recursive algorithm is implemented above. The function permutations gets an array and for each item recursively gets all permutations beginning with current item. At each step of recursion the array is shorter by one item (minus the current item) and at the last step single element array is not being processed because permutations are not available anymore.

Also some comments added to the code.

The last line is just the test to get all permutations of array [1, 2, 3] and to show them via alert. To get more illustrative view all found permutations are glued with new line symbol (.join("\n")).

Rango
  • 3,728
  • 2
  • 20
  • 32
0

As stated by the comments and the above answer you need permutations operation. However there are many ways to obtain the permutations of an array. For further information on permutations i would advise you to have a look at Permutations in JavaScript topic.

On the other hand a recursive approach is always much slower compared to a dynamical programming approach. Recently i have come up with a permutations algorithm which seems to be the fastest of all. Check it up

Community
  • 1
  • 1
Redu
  • 19,106
  • 4
  • 44
  • 59