Posted by & filed under Code Tips.

Found this one on Stack Overflow needing a quick and easy way to create somewhat random strings.

function randomString(len, charSet) {
    charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var randomString = '';
    for (var i = 0; i < len; i++) {
    	var randomPoz = Math.floor(Math.random() * charSet.length);
    	randomString += charSet.substring(randomPoz,randomPoz+1);
    }
    return randomString;
}

If you visit the entire thread where this code was posted there’s a pretty good discussion of how to create random strings using math and date functions.

Leave a Reply

Your email address will not be published. Required fields are marked *