blog

TIL: Random Id

2020-06-09

TIL Number.toString() in javascript tags a radix, which is a handy way to make an short alphanumeric random id.

Occasionally I need to generate an id and end up doing something like:

1
Math.floor(Math.random() * 1000000000)

which just feels a little hackish, but generating a full UUID seems like overkill. But this generate a short random alphanumeric string:

1
Math.random().toString(36).substr(2)

Which gives you something like “d6gykt0qjmu” a decent simple random id, and feels alot more elegant.