This article mainly introduced the JavaScript array random arrangement realizes the random shuffle function the method, relates to the JavaScript to realize the array random arrangement technique based on the List.sort method, may apply in the random shuffle, very has the practical value, the need friend may refer to under
In this paper, we describe the method of random shuffling of JavaScript array to realize random shuffle function. Share to everyone for your reference. The specific analysis is as follows:
This JS code can be in the array of elements of random arrangement, this is very useful, such as we play poker when you can arrange cards, that is, computer shuffle.
?
1 2 3 |
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 help you with your JavaScript programming.