Nodejs Random String Get randomString(bits)

Here you can find the source of randomString(bits)

Method Source Code

function randomString(bits){
    var chars,rand,i,ret
      chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
      ret=''/*  w w w.  ja va  2s. c  o  m*/
      // in v8, Math.random() yields 32 pseudo-random bits (in spidermonkey it gives 53)
      while(bits > 0){
        rand=Math.floor(Math.random()*0x100000000) // 32-bit integer
        // base 64 means 6 bits per character, so we use the top 30 bits from rand to give 30/6=5 characters.
        for(i=26; i>0 && bits>0; i-=6, bits-=6) ret+=chars[0x3F & rand >>> i]}
      return ret
}
console.log(0x3F);
console.log(randomString(30))

var test = function(){
   
   /*
    hello world
    */
};

var s = new String(test);
console.log(s.substring(s.indexOf("/*") + 3, s.lastIndexOf("*/")));


Function.prototype.getstr = function() {  
    var lines = new String(this);  
    lines = lines.substring(lines.indexOf("/*") + 3, lines.lastIndexOf("*/"));  
    return lines;  
};

String.prototype.$ = function(r, v){ return this.replace(new RegExp('<!-- ' + r + ' -->', 'g'), v);};
String.prototype.trim = function() { return this.replace(/^\s+/g,"").replace(/\s+$/g,""); };

Related

  1. randomAnagram()
    String.prototype.randomAnagram = function () {
      return this.split('').sort(function () {
        return 0.5 - Math.random();
      }).join('');
    };
    
  2. randomBetween(min, max)
    String.prototype.randomBetween = function(min, max) {
      var hash = this.getHashCode();
        min = min || 0;
        max = max || 1;
        var seed = (hash * 9301 + 49297) % 233280;
        var rnd = Math.abs(seed / 233280.0);
        return min + rnd * (max - min);
    };
    
  3. randomString(len, charSet)
    helpers.randomString = function(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;
    };
    ...
    
  4. randomString(len, charSet)
    String.randomString = function {
        "use strict";
        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;
    ...
    
  5. randomString(length)
    function randomString ( length ) {
      var seed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
      var name = "";
      for ( var i = 0; i < length; i++ ) {
        name += seed.charAt( Math.floor( Math.random() * seed.length ) );
      return name;
    
  6. randomString(stringLength)
    String.prototype.randomString = function(stringLength) {
      var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
      if (!stringLength>0) {
        var stringLength = 8;
      var randomString = '';
      for (var i=0; i<stringLength; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomString += chars.substring(rnum,rnum+1);
    ...