``````/**
* Generate an ID that does not duplicate
*/
function GenNonDuplicateID():String{

}
``````

## Let's look at the following methods

### 1. Math.random, which generates [0,1] random numbers, such as

``````//I run this time to generate: 0.5834165740043102
Math.random()
``````

### 2. Get the current timestamp Date.now

``````//Now the timestamp is 148645606622
Date.now() = 1521009303858
``````

### 3. Convert 10-digit to other-digit strings Number.toString

``````//Converting 1482645606622 to binary: 101011001101001001001001001000100100001011011110
(1482645606622).toString(2)
//Convert to hexadecimal: The ObjectID in 159349090 de MongDB is a 24-bit hexadecimal number
(1482645606622).toString(16)；
//Maximum support for conversion to hexadecimal, using characters of 0-9a-z: ix48wvry
(1482645606622).toString(36)
``````

## Self-Evolution of GenNonDuplicateID

### 1. Random Number Version v0.0.1

``````/**
* Generate an ID that does not duplicate
*/
function GenNonDuplicateID(){
return Math.random().toString()
}

//Generate an ID similar to 0.1283460319177394
GenNonDuplicateID()
``````

### 2. Random Number Version 16-ary Version v0.0.2

``````/**
* Generate an ID that does not duplicate
*/
function GenNonDuplicateID(){
return Math.random().toString(16)
}

//The function generates an ID similar to 0.c1615913fa915
GenNonDuplicateID()
``````

### 3. Random Number Version 36 Binary Version v0.0.3

``````/**
* Generate an ID that does not duplicate
*/
function GenNonDuplicateID(){
return Math.random().toString(36)
}

//The function generates an ID similar to 0.hefy7uw6ddzwidkwcmxkzkt9
GenNonDuplicateID()
``````

### 4. Remove "0." v0.0.4 from the 36-digit version of the random number version

``````/**
* Generate an ID that does not duplicate
*/
function GenNonDuplicateID(){
return Math.random().toString(36).substr(3)
}

//The function generates an ID similar to 8dlv9vabygks2cbg1spds4i
GenNonDuplicateID()
``````

However, when a random number is used as an ID, the same two IDs will inevitably appear as the number of times used accumulates.

### 5. The 36-decimal version v0.1.1 introducing timestamp

``````/**
* Generate an ID that does not duplicate
*/
function GenNonDuplicateID(){
let idStr = Date.now().toString(36)
idStr += Math.random().toString(36).substr(3)
return idStr
}

//The function generates an ID similar to ix49sfsnt7514k5wpflyb5l2vtok9y66r
GenNonDuplicateID()
``````

### 6. Introducing 36-decimal version of timestamp and adding random number length control v0.1.2

``````/**
* Generate an ID that does not duplicate
*/
function GenNonDuplicateID(randomLength){
let idStr = Date.now().toString(36)
idStr += Math.random().toString(36).substr(3,randomLength)
return idStr
}

// GenNonDuplicate ID (3) will generate an ID similar to ix49wl2978w
GenNonDuplicateID(3)
``````

The first few ID s generated in this way are always the same. They look bad, so they are changed again.

### 7. Introducing a time stamp random number preceding 36 digits into random number length control v0.1.3

``````/**
* Generate an ID that does not duplicate
*/
function GenNonDuplicateID(randomLength){
return Number(Math.random().toString().substr(3,randomLength) + Date.now()).toString(36)
}
//GenNonDuplicateID() generates rfmipbs 8ag0kgcogc-like IDs
GenNonDuplicateID()
``````