mercredi 11 septembre 2019

How to generate a random string from ASCII characters in JavaScript

How can I build a simple string generator that creates and prints out a randomized string of 10 characters, including lowercase and uppercase letters, numbers and special characters from 0 to 127 of the ASCII table by typing the ASCII-character-number-range in the method? Not a variable like

    var possibleCharacters = "01324567890abcdefgh....."

A friend of me already built it in Java (see below), so how can I build that in JavaScript, also with a for-loop like in the Java example?

    public class Main {
public static void main(String[] args) {
    for (int counter = 0; counter <= 9; counter++) {
        int randomNum = 0 + (int)(Math.random() * 127);
        if(randomNum > 33) {
            System.out.print((char)randomNum);
        }else {
            counter--;
        }
    }
}
}

It should just generate something like "_e7N?:G&M0" i.e.




Aucun commentaire:

Enregistrer un commentaire