Skip to content

Instantly share code, notes, and snippets.

@Pengeszikra
Created April 28, 2016 08:45
Show Gist options
  • Select an option

  • Save Pengeszikra/9dcba0ac9d33e0dcab16776454648732 to your computer and use it in GitHub Desktop.

Select an option

Save Pengeszikra/9dcba0ac9d33e0dcab16776454648732 to your computer and use it in GitHub Desktop.
five digit random radix32 ID ES6
/* five digit random radix32 ID */
function GUID32(){ return ('0'.repeat(5)+( ~~(Math.random() * parseInt( 10e4 , 32 )) ).toString(32).toUpperCase()).slice( -5 ) };
@Pengeszikra
Copy link
Author

Pengeszikra commented Apr 7, 2017

regexp version

Math.random().toString(36).match(/[a-zA-Z].{7}/)[0].toUpperCase();

test case: chance for Math.random().toString(36) hold only numbers

var res={}
for(i=0;i<1e8;i++){ 
  let uidl = Math.random().toString(36).match(/[a-zA-Z].*/)[0].length
  res[uidl] = res[uidl] ? res[uidl]+1 : 1
}

result of 1e8

Length Amount
11 3
12 8
13 45
14 118
15 456
16 1634
17 5444
18 18538
19 64880
20 221587
21 753892
22 2528219
23 8378365
24 27260386
25 56597050
26 4169375

@Pengeszikra
Copy link
Author

Pengeszikra commented Aug 31, 2017

simpliest without ID length

Math.random().toString(32).slice(2).toUpperCase()

@Pengeszikra
Copy link
Author

Base64 random string generator without pattern

btoa(Math.random()).slice(3,-2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment