Javascript generates random numbers that are not repeated, and javascript random numbers.
Source of question: one test question when learning jQuery in MOOC.
Initially: only five <li> elements are displayed in the <ul> element, including the last <li> element and "more" characters in the <a> element. when you click "more", the content becomes "simplified", and all <li> elements are displayed in the <ul> element. when you click the "simplified" link, the content of the link changes to "more". At the same time, the <ul> element only displays the five elements including the last <li> element.
Core point: He didn't talk about the <li> elements to be hidden, so I want to list eight <li> elements, and simply hide three of the first seven <li> elements randomly.
Ideas:
① From 0 ~ Generate three random numbers in Step 6.
② Determine whether the three random numbers are equal. If they are not equal, perform the hidden operation.
③ If a random number already exists, re-execute the function.
Implementation: Generate 0 ~ A Random Number of 6
Copy codeThe Code is as follows: var ran1 = parseInt (Math. random () * 7); // Math. random () generates a random number [)
Complete code:
<! DOCTYPE html>
Tip 1:
Copy codeThe Code is as follows: var ran = parseInt (Math. random () * (max-min + 1) + min); // generates a random number in the [min, max] interval
Tip 2:
After reflecting on this, I decided to write an encapsulation function in a certain range of [min, max] to generate n non-repeated random numbers.
Train of Thought 1: Count to n random numbers in the [min, max] interval, and compare whether there are duplicates. If there are duplicates, return and execute again.
Demo address: http://jsbin.com/yupuyehuqa/edit? Html, js, output
Encapsulation function:
function my_ran(n,min,max){ var arr=[]; for(i=0;i<n;i++){ arr[i]=parseInt(Math.random()*(max-min+1)+min); } for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(arr[i]==arr[j]){ my_ran(n,min,max); return fault; } } } return arr;}
Train of Thought 2: generate the random number of the I [min, max] interval, and compare with the number of the previous I-1, if there is repetition, make I = I-1; repeatedly generate the I random number.
Demo address: http://jsbin.com/zorunotosi/edit? Html, js, output
Encapsulation function:
function my_ran2(n,min,max){ var arr=[]; for(i=0;i<n;i++){ arr[i]=parseInt(Math.random()*(max-min+1)+min); for(j=0;j<i;j++){ if(arr[i]==arr[j]){ i=i-1; break; } } } return arr;}
Train of Thought 3: generate an ordered array of [min, max] intervals, disrupt the array, and output the first n values.
Demo address: http://jsbin.com/zorunotosi/edit? Html, js, output
Encapsulation function:
function my_ran3(n,min,max){ var arr=[]; var arr2=[]; for(i=0;i<max-min+1;i++){ arr[i]=i+min; } for(var j,x,i=arr.length;i;j=parseInt(Math.random()*i),x=arr[--i],arr[i]=arr[j],arr[j]=x); for(i=0;i<n;i++){ arr2[i]=arr[i]; } return arr2;}
Idea 4: generate an ordered array of [min, max] intervals, randomly select a value from it, delete the value from the array, and then select the second random value.
Demo address: http://jsbin.com/zorunotosi/edit? Html, js, output
Encapsulation function:
function my_ran4(n,min,max){ var arr=[]; var arr2=[]; for(i=0;i<max-min+1;i++){ arr[i]=i+min; } for(i=0;i<n;i++){ var x=parseInt(Math.random()*arr.length); arr2[i]=arr[x]; for(j=x;j<arr.length;j++){ arr[j]=arr[j+1]; } arr.length=arr.length-1; } return arr2;}
It's too late. Tomorrow we have time to adjust the format.
The above is all the content of this article. I hope you will like it.