JavaScript array random arrangement to achieve a random shuffle function

  • 2020-05-17 04:46:22
  • OfStack

The example of this paper describes the random arrangement of JavaScript array to realize the function of random shuffling. Share with you for your reference. The specific analysis is as follows:

This JS code allows you to randomly arrange the elements in an array, which is very useful, for example, when you're playing poker, you can arrange the CARDS, which is a computer shuffle.


var list = [1,2,3,4,5,6,7,8,9];
list = list.sort(function() Math.random() - 0.5);
Print(list); // prints something like: 4,3,1,2,9,5,6,7,8

I hope this article is helpful for you to design javascript program.


Related articles: