Generate random stringcharacters in JavaScript

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9].

What s the best way to do this with JavaScript?

Answers

I think this will work for you:

function makeid()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils