rainbowcorn / Random Generator

// ==UserScript==
// @name            Random Generator
// @description     
// @author          rainbowcorn
// @namespace       RandomGenerator
// @homepageURL     https://openuserjs.org/scripts/rainbowcorn/RandomGenerator
// @include         http://www.google.*
// @include         https://www.google.*
// @include         https://encrypted.google.*
// @run-at          document-start
// @version         0.0.6
// @license         MIT
// @noframes
// ==/UserScript==

var Rand = function () {
    this.chars = {
        number: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
        lower: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
        upper: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
        symbol: ['~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '\'', '"', ',', '.', '/', '<', '>', '?']
    };
};

Rand.prototype.int = function () {
    let count = 1, i, len = arguments.length, max = 2147483647, min = 0, results = [], tmp;
    switch (len) {
        case 1:
            if (parseInt(arguments[0]) > 1) {
                count = parseInt(arguments[0]);
            }
            break;
        case 2:
        case 3:
            if (parseInt(arguments[0]) > parseInt(arguments[1])) {
                max = parseInt(arguments[0]);
                min = parseInt(arguments[1])
            } else {
                max = parseInt(arguments[1]);
                min = parseInt(arguments[0]);
            }

            if (len === 3) {
                if (parseInt(arguments[2]) > 1) {
                    count = parseInt(arguments[2]);
                }
            }
            break;
    }

    tmp = Math.floor(Math.random() * max) + min;
    while (results.length < count) {
        results.push(tmp);
        tmp = Math.floor(Math.random() * max) + min;
    }
    return results.length > 1 ? results : results[0];
};

Rand.prototype.str = function () {
    let argsAmount = arguments.length, chars, count = 1, max = 32, min = 4, results = [], tmp;
    chars = this.chars.lower.concat(this.chars.upper).concat(this.chars.symbol).concat(this.chars.number);

    function rs (c, l) {
        let r = '';
        for (let i = 0; i < l; i ++) {
            r += c[Math.floor(Math.random() * c.length)];
        }
        return r;
    }

    switch (argsAmount) {
        case 0:
            len = Math.floor(Math.random() * max) + min;
            break;
        case 1:
        case 2:
            len = parseInt(arguments[0]);
            if (argsAmount === 2 && typeof arguments[1] === 'string') {
                switch (arguments[1].trim().toLowerCase()) {
                    case 'hex':
                        chars = this.chars.number.concat(['a', 'b', 'c', 'd', 'e', 'f']);
                        break;
                    case 'alphanum':
                        chars = this.chars.lower.concat(this.chars.upper).concat(this.chars.number);
                        break;
                    case 'alphanumlower':
                        chars = this.chars.lower.concat(this.chars.number);
                        break;
                    case 'alphanumupper':
                        chars = this.chars.upper.concat(this.chars.number);
                        break;
                    case 'symbol':
                        chars = this.chars.symbol;
                        break;
                    case 'binary':
                        chars = [0, 1];
                        break;
                }
            }
            break;
    }

    tmp = rs(chars, len);
    while (results.length < count) {
        results.push(tmp);
        tmp = rs(chars, len);
    }
    return results.length > 1 ? results : results[0];
};

Rand.prototype.hex = function () {
    let len = typeof arguments[0] === 'number' ? parseInt(arguments[0]) : Math.floor(Math.random() * 4) + 32;
    return this.str(len, 'hex');
};