jeudi 29 septembre 2016

Is it possible to shuffle the values in an array in random order? [duplicate]

This question already has an answer here:

I have the following array in javascript.

    var numbers = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8];

I'm trying to get these numbers in a random order in an array, the array has to have a size of 0-15 and has to contain these numbers. I have tried using different functions to generate random numbers between 1-8 and add them in the array but I cannot figure out how to have exactly two of each of these numbers.

I.E. the result has to be (in random order):

    var numbers = [5,4,2,1,3,6,5,4,2,1,3,6,7,8,7,8];

Is there a convenient way to do this in javascript?




Aucun commentaire:

Enregistrer un commentaire