Skip to content

Instantly share code, notes, and snippets.

@Dreyer
Created April 12, 2012 15:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Dreyer/2368164 to your computer and use it in GitHub Desktop.
Save Dreyer/2368164 to your computer and use it in GitHub Desktop.
Generate random alphanumeric string in JavaScript
// @source: http://stackoverflow.com/questions/1349404/generate-a-string-of-5-random-characters-in-javascript
function randomStr(m) {
var m = m || 9; s = '', r = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i=0; i < m; i++) { s += r.charAt(Math.floor(Math.random()*r.length)); }
return s;
};
@jeremiaspereira
Copy link

Muito bom, usei ele para gerar cor.

@NeuralNoise
Copy link

is there any specific code to say output to a php or html, and can this be modded to save a txt or xml file too?
anyway thank you for sharing your file.

@elnygren
Copy link

elnygren commented Jun 29, 2017

@hemanthkumarcb
Copy link

try this npm i random-key-generator

@Ilya33
Copy link

Ilya33 commented May 23, 2018

why variables s and r are global?
another solution for this task: http://javascript-benchmark.info/t/generate-random-string

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