JavaScript Random Strings, Random Number

JavaScript generate random strings, javaScript random number random characters functions

1

JavaScript Random Strings And Numbers

fHnngj83sS3Yl1GIFk76
JavaScript
function random_chars(length = 0) {
    let result = "";
    let chars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (var i = 0; i < length; i++) {
        result += chars[Math.floor(Math.random() * chars.length)];
    }
    return result;
}

function get_random_chars(length) {
    let result = random_chars(length);
    document.getElementById("random-char-text").innerText = result;
}
HTML
<input type="button" onclick="get_random_chars(10)" value="random text">
<div id="random-char-text"></div>
Try it Yourself
2

JavaScript Random Number

06038139811258919436
JavaScript
function random_numbers(length = 0) {
    let result = "";
    let chars = "1234567890";
    for (let i = 0; i < length; i++) {
        result += chars[Math.floor(Math.random() * chars.length)];
    }
    return result;
}

function get_random_number(length) {
    let result = random_numbers(length);
    document.getElementById("random-number-text").innerText = result;
}
HTML
<input type="button" onclick="get_random_number(10)" value="random numbers">
<div id="random-number-text"></div>
Try it Yourself
3

JavaScript Random Letters Lowercase Only

kwilwrugqxosyztnzawg
JavaScript
function random_lowercase(length = 0) {
    let result = "";
    let chars = "abcdefghijklmnopqrstuvwxyz";
    for (var i = 0; i < length; i++) {
        result += chars[Math.floor(Math.random() * chars.length)];
    }
    return result;
}

function get_random_lowercase(length) {
    let result = random_lowercase(length);
    document.getElementById("random-lower-text").innerText = result;
}
HTML
<input type="button" onclick="get_random_lowercase(10)" value="random text">
<div id="random-lower-text"></div>
Try it Yourself
4

JavaScript Random Letter Uppercase Only

ZPZXMVZDJCBROJAYRIPN
JavaScript
function random_uppercase(length = 0) {
    let result = "";
    let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (let i = 0; i < length; i++) {
        result += chars[Math.floor(Math.random() * chars.length)];
    }
    return result;
}

function get_random_uppercase(length) {
    let result = random_uppercase(length);
    document.getElementById("random-upper-text").innerText = result;
}
HTML
<input type="button" onclick="get_random_uppercase(10)" value="random text">
<div id="random-upper-text"></div>
Try it Yourself
5

JavaScript Random Number Between

1500
JavaScript
function number_between(min_number, max_number) {
    let min = Math.ceil(min_number);
    let max = Math.floor(max_number + 1);
    let result = Math.floor(Math.random() * (max - min)) + min;
    return result;
}

function get_number_between(min, max) {
    let result = number_between(min, max);
    document.getElementById("number-btwn-text").innerText = result;
}
HTML
<input type="button" onclick="get_number_between(10, 20)" value="Number between">
<div id="number-btwn-text"></div>
Try it Yourself
6

JavaScript Generate Random Password

RIA~]Bq;g$~NGt#(35n!
JavaScript
function random_password(length = 8) {
    let result = "";
    let chars = "1234567890";
    chars += "abcdefghijklmnopqrstuvwxyz";
    chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    chars += "~!@#$%^&*()_+}{></[]|-,:;'\".?";
    for (var i = 0; i < length; i++) {
        result += chars[Math.floor(Math.random() * chars.length)];
    }
    return result;
}

function get_random_password(length) {
    let result = random_password(length);
    document.getElementById("random-password").innerText = result;
}
HTML
<input type="button" onclick="get_random_password(10)" value="Random Password">
<div id="random-password"></div>
Try it Yourself