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.