[JavaScript] Generate Random String From [a-z0-9]


/**
 * Returns a random integer between min (inclusive) and max (inclusive)
 * Using Math.round() will give you a non-uniform distribution!
 * Code from http://stackoverflow.com/a/1527820
 */
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

/**
 * Input: the length of random string
 * Output: a random string from [a-z0-9]
 */
function RandomString(strlen) {
    const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    var result = "";
    for (var i=0; i<strlen; i++) {
        result += chars[getRandomInt(0,35)];
    }
    return result;
}

// function call to generate a random string of length 10
RandomString(10);

Run code online:


Tested on: Chromium Version 55.0.2883.87 Built on Ubuntu , running on Ubuntu 16.10 (64-bit).


References:

[1]

javascript random integer - Google search

javascript random integer - DuckDuckGo search

javascript random integer - Bing search

javascript random integer - Yahoo search

javascript random integer - Baidu search

javascript random integer - Yandex search

[2]Generating random whole numbers in JavaScript in a specific range? - Stack Overflow
[3]

javascript constants - Google search

javascript constants - DuckDuckGo search

javascript constants - Bing search

javascript constants - Yahoo search

javascript constants - Baidu search

javascript constants - Yandex search

[4]How to declare string constants in JavaScript? - Stack Overflow
[5][Golang] Generate Random String From [a-z0-9]