3

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 = "01234567890abcdefgh....."

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.

8
  • 2
    What is your attempt? You can use charCode. Commented Sep 12, 2019 at 4:27
  • @YongQuan Thanks, this looks good. Unfortunately as a newbie I don't have any clue how to write that method. I also don't understand the code of my friend, and why he wrote a 33 in the code. I have no clue how to write that whole password generator function Commented Sep 12, 2019 at 4:33
  • A function to to return the random string, fiddle Commented Sep 12, 2019 at 4:36
  • Thank you so much @shrys it's working! How can I upvote your comment as it is right below my thread and not in the general answers section? Commented Sep 12, 2019 at 4:39
  • 1
    @MrFrontend if you are planning to implement a password generator function, you CANNOT generate random string this way because Math.random() is NOT cryptographically secure. You have to use the Web Crypto API. Commented Sep 12, 2019 at 6:13

1 Answer 1

1

A function to return the random string:

function getString() {
  var str = "";
  for (counter = 0; counter <= 9; counter++) {
    var randomNum = 0 + parseInt(Math.random() * 127);
    if (randomNum > 33) {
      str += String.fromCharCode(randomNum);
    } else {
      counter--;
    }
  }
  return str;
}
for (i = 0; i < 10; i++)
  console.log(getString());

If you're attempting to generate numbers between 33 and 127:

function getString() {
  var str = "";
  for (counter = 0; counter <= 9; counter++) {
    var randomNum = 0 + parseInt(Math.floor(Math.random() * (127 - 33 + 1) + 33));
    str += String.fromCharCode(randomNum);
  }
  return str;
}
for (i = 0; i < 10; i++)
  console.log(getString());

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks again. Can you tell me in the fourth line (of both examples) what the "0" in 0 + parseInt is about? When I delete it, it seems it doesn't change anything.
Yes I think it doesn't do anything, might as well omit it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.