Example method for generating non-duplicate data randomly in JS _ javascript skills

Source: Internet
Author: User
This article introduces how to randomly generate instances with no duplicate data in JS, if you have any questions, please refer to the article I read on IBM last night about how Java developers think about JavaScript, as a Java developer, you must understand and be proficient in JavaScript. after all, JavaScript is very powerful. After Jquery, Ext, and other plug-ins are injected. it has been gradually favored by Java developers again.
As required by the project, a method for generating random numbers is specially compiled, which is not repeated.
The Code is as follows:
Javascript code

The Code is as follows:


// 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 );
}


Copy to the developer tool in the FireFox browser: Run in "" to view the result.
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.