Skip to content

Instantly share code, notes, and snippets.

@UniBreakfast
Created November 29, 2019 16:14
Show Gist options
  • Save UniBreakfast/2ca771944ebabcfcd86800b27aafdabb to your computer and use it in GitHub Desktop.
Save UniBreakfast/2ca771944ebabcfcd86800b27aafdabb to your computer and use it in GitHub Desktop.
random strings for session tokens etc
// this one is shorter
rndStr1 = (i=4, f=()=>Math.random().toString(36).slice(2), s='') =>
{ for (;i;--i) s+=f(); return s }
// this one is faster
rndStr2 = (n=42,
l='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz--', s='') =>
{ for (;n;--n) s+=l[Math.random()*62|0]; return s }
// this one is with dashes
rndStr3 =()=> rndStr2(36).match(/\w{6}/g).join`-`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment