Create random string in typescript

Viewed 6001

How do I create a random string in typescript consisting of the alphabet [a-z0-9]? It should always consist of 32 digits. And also there should be no redundant strings.

1 Answers

Try this:

makeString(): string {
    let outString: string = '';
    let inOptions: string = 'abcdefghijklmnopqrstuvwxyz0123456789';

    for (let i = 0; i < 32; i++) {

      outString += inOptions.charAt(Math.floor(Math.random() * inOptions.length));

    }

    return outString;
  }

  result: string = this.makeString();
Related