Home >Web Front-end >JS Tutorial >JavaScript arrays are randomly arranged to implement random shuffling function_javascript skills

JavaScript arrays are randomly arranged to implement random shuffling function_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:08:341176browse

The example in this article describes the method of randomly arranging JavaScript arrays to implement random shuffling function. Share it with everyone for your reference. The specific analysis is as follows:

This JS code can randomly arrange the elements in the array. This is very useful. For example, when we play poker, we can arrange the poker cards, which is the computer shuffling the cards.

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 will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn