Home  >  Article  >  Web Front-end  >  JS instance method to randomly generate non-duplicate data_javascript skills

JS instance method to randomly generate non-duplicate data_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:28:35945browse

Last night I read an article on IBM about Java developers’ views on JavaScript. I felt deeply about it and found that as a Java developer, you should understand and be proficient in JavaScript. After all, JavaScript is very powerful now. After the injection of plug-ins such as Jquery and Ext, it has gradually become favored by Java developers again.
Due to the needs of the project, I have specially written a method to generate random numbers without duplication.
The code is as follows :
Javascript code

Copy code The code is as follows:

// Define storage to generate random Array of numbers
var array=new Array();
// Loop N times to generate random numbers
for(var i = 0; ; i ){
// Only generate 10 random numbers
if(array.length<10){
generateRandom(10);
}else{
break;
}
}
// Loop through the random number array
for(var i = 0 ; i < array.length; i ){
alert(array[i]);
}
// Method to generate random numbers
function generateRandom( count){
var rand = parseInt(Math.random()*count);
for(var i = 0; i < array.length; i ){
if(array[i] = = rand){
                                                                           ; Run it in Tools: "Scratchpad" to view the results.
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