Global.String.random (len, mode)
creates a random string (numbers and chars)

  • Parameter len:
    length of key
  • Parameter mode:
    determines which letters to use. null or 0 = all letters; 1 = skip 0, 1, l and o which can easily be mixed with numbers; 2 = use numbers only
  • Returns
    random string
Sourcecode in /home/hannes/helma/modules/core/String.js:
82:   String.random = function(len, mode) {
83:       if (mode == 2) {
84:           var x = Math.random() * Math.pow(10,len);
85:           return Math.floor(x);
86:       }
87:       var keystr = String.NULL;
88:       for (var i=0; i<len; i++) {
89:           var x = Math.floor((Math.random() * 36));
90:           if (mode == 1) {
91:              // skip 0,1
92:              x = (x<2) ? x + 2 : x;
93:              // don't use the letters l (charCode 21+87) and o (24+87)
94:              x = (x==21) ? 22 : x;
95:              x = (x==24) ? 25 : x;
96:          }
97:          if (x<10) {
98:              keystr += String(x);
99:          }    else    {
100:              keystr += String.fromCharCode(x+87);
101:          }
102:      }
103:      return keystr;
104:  }