JavaScript gets a random number of four digits or letters _javascript tips

Source: Internet
Author: User

This section describes how to implement a simple four-bit random number function by using a code example.
A simpler way to do this is to randomly extract four characters from numbers and letters.

The code example is as follows:

function only (Ele,arr) { 
 if (arr.length==0) { 
  true; 
 } 
 for (Var j=0;j<arr.length;j++) { 
  if (Ele==arr[j]) {return 
   false; 
  } else{return 
   true; 
  } 
 } 
 
var arr=[0,1,2,3,4,5,6, "A", "B", "C", "D", "E", "F", "G"]; 
 
(function () { 
 var randnum=null; 
 var old=[]; 
 var str= ""; 
 function done () { 
  Randnum=math.floor (math.random () *14); 
  if (only (Randnum,old)) { 
   str=str+arr[randnum]; 
   Old.push (Randnum); 
  } 
  else{done 
   (); 
  } 
 for (Var index=0;index<4;index++) {done 
  (); 
 } 
 Console.log (str); 
}) (arr)

The above code to achieve our requirements, the following describes the implementation of the code above.


A. Code comments:
1.function only (Ele,arr) {}, which enables you to determine whether the specified index has been used, and to place random numbers in duplicate.
2.if (arr.length==0) {}, returns true if the array is empty and the case cannot be duplicated.
3.for (Var j=0;j<arr.length;j++) {}, the array is not empty, it traverses the elements in the array, makes the comparison, returns true without repetition, otherwise returns false. 4.var arr=[0,1,2,3,4,5,6, "a", "B", "C", "D", "E", "F", "G"], get an array of random numbers, and of course can be expanded.
5. (function () {}) (arr), a self execution function, and passing a parameter.
6.var Randnum=null, declares a variable and assigns the initial value to NULL to store the randomly generated array index.
7.var old=[], creates an empty array to store the array index values that have already occurred.
8.var str= "" To create an empty string that holds the generated random number.
9.function done () {}, this function can be used to get a random number.
10.randnum=math.floor (Math.random () *14), gets the index value of the array.
11.if (only (Randnum,old)) {
Str=str+arr[randnum];
Old.push (Randnum);
To determine if it has been used, if not, get the element of the array, append to the STR string, and finally append the index value to the old array.
12.else{done ();
If you've already used it, get it again, here's how to use recursion.
13.for (var index=0;index<4;index++) {
Done ();
, use a For loop to get 4 random numbers.

From: http://www.softwhy.com/forum.php?mod=viewthread&tid=16493

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.