tgagvw
Last Updated: February 25, 2016
·
352
· richardgong1987
Psb  1

get random any length string from [1-10,a-z,A-Z]

var _random = function (length) {
                var s = '';
                var randomchar = function () {
                    var myran = Math.random();
                    var myf = myran * 62;
                    var myff = Math.floor(myf);
                    console.log('myran:',myran, 'myran * 62:',myf, 'Math.floor(myf)', myff);
                    var n = myff;
                    if (n < 10) return n; //1-10
                    if (n < 36) return String.fromCharCode(n + 55); //A-Z
                    return String.fromCharCode(n + 61); //a-z
                }
                while (s.length < length) s += randomchar();
                return s;
            }
            console.log(_random(15));//CXBlhRIxvUeRGE6

what’s the “62”? cos, ‘a-z,A-Z, 1-10’have 62 charater

Say Thanks
Respond