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
// 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.