JavaScript generates _JAVASCRIPT techniques for random numbers with no duplicates

Source: Internet
Author: User

Source: A test question in the process of learning jquery in Mu class.

Initially, only 5 <li> elements are displayed in the:<ul> element, which includes the display of "more" characters in the last <li> element,<a> element.

When you click on the "More" link, the content becomes "simplified" while the,<ul> element displays all the <li> elements.

When you click on the "Simplify" link, your content becomes "more", while the,<ul> element displays only 5 elements, including the last <li> element.

Core point: He didn't tell me what a few <li> elements to hide, so I wanted to list 8 <li> elements, and point to simplify the 3 of the first 7 <li> elements that were randomly hidden.

Ideas:

① generates 3 random numbers from the 0~6.

② to determine whether 3 random numbers are equal and unequal then perform a hidden operation.

③3 The random number has a duplicate, the function is executed again.

Implementation: Generates a random number of 0~6

Copy Code code as follows:
var ran1=parseint (Math.random () *7); Math.random () A random number that generates [0,1]

Complete code:

<! DOCTYPE html>  

Harvest One:

Copy Code code as follows:
var ran=parseint (Math.random () * (max-min+1) +min); Generates a random number for a [Min,max] interval

Harvest Two:

In retrospect, the decision to write a package function that generates n a random number of not duplicates in a certain [Min,max] interval.

Idea one: Sir into n [Min,max] interval random number, compare whether repeat, if repeat, return, execute again.

Demo Address: Http://jsbin.com/yupuyehuqa/edit?html,js,output

Encapsulate 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;
}

Idea two: Generate the random number of the first [Min,max] interval, and compare with the number of previous i-1, if there is repetition, make i=i-1, repeat generation I random number.

Demo Address: Http://jsbin.com/zorunotosi/edit?html,js,output

Encapsulate 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;
}

Idea three: Generate a sequential array of [min,max] intervals, disrupt the array, and output the first n values.

Demo Address: Http://jsbin.com/zorunotosi/edit?html,js,output

Encapsulate 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 four: Generate a sequential array of [Min,max] intervals from which to randomly select a value, then delete the value in the array, and then select the second random value.

Demo Address: Http://jsbin.com/zorunotosi/edit?html,js,output

Encapsulate 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 there is no time to adjust the format.

The above mentioned is the entire content of this article, I hope you can enjoy.

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.