JS disrupt the Array (random sorting to generate random numbers that are not repeated)

Source: Internet
Author: User

The method to achieve random sorting of arrays is actually very simple, but Baidu found that there are a large number of such instances, but I strongly recommend these three instances. The random sorting written by a foreign user is not repeated, it's not bad. Let's take a look.

I think it is the most streamlined method that has been written by foreign people on the Internet:

Program code

// Use the Math. random () function to generate 0 ~ The random number between 1 and 0.5 is compared, and-1 or 1 is returned.

The Code is as follows: Copy code

Function randomsort (a, B ){
Return Math. random ()>. 5? -1: 1;
}
Var arr = [1, 2, 3, 4, 5];
Var arr2 = arr. sort (randomsort );
Alert (arr2 );

Here we will introduce the sort () function. In JS, an Array object has a built-in function:

Arrayobj. sort ([sortfunction])

This method sorts Array objects appropriately. During execution, no new Array objects are created.

SortFunction is optional.

Is the name of the function used to determine the order of elements. If this parameter is omitted, the elements are listed in ascending order of ASCII characters.
The sortFunction method has two parameters. Represent the two array items for each sort comparison. This parameter is executed every time two array items are compared during sort () sorting, and two compared array items are passed as parameters to this function. When the return value of the function is 1, the order of two array items is exchanged. Otherwise, the order is not exchanged.

Some of my domestic friends wrote

The Code is as follows: Copy code

// Obtain the random number in the array
// HF. Math. RandomNumbers is the prefix and can be defined by yourself. It mainly depends on the logic code.
HF. Math. RandomNumbers = function (startNum, endNum, count, repeat ){
Var ret = [];
If (repeat ){
For (var I = 0; I <count; I ++ ){
Ret [I] = HF. Math. Random (startNum, endNum );
}
Return ret;
}
Var tmp = [];
Var I = 0;
For (var s = startNum; s <= endNum; s ++ ){
Tmp [I ++] = s;
}
Var l = tmp. length;
For (I = 0; I <count; I ++ ){
Ret [I] = HF. Array. Remove (tmp, HF. Math. Random (0, -- l ));
}
Return ret;
}

Examples of Generating Random Arrays

The Code is as follows: Copy code

// Define an array for storing the random numbers generated
Var array = new Array ();
 
// Generate a random number N times in a loop
For (var I = 0; I ++ ){
// Generate only 10 random numbers
If (array. length <10 ){
GenerateRandom (10 );
} Else {
Break;
}
}
 
// Cyclically traverse the random number Array
For (var I = 0; I <array. length; I ++ ){
Alert (array [I]);
}
 
// Method for generating random numbers
Function generateRandom (count ){
Var rand = parseInt (Math. random () * count );
For (var I = 0; I <array. length; I ++ ){
If (array [I] = rand ){
Return false;
}
}
Array. push (rand );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.