3

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.

Michael
  • 287
  • 1
  • 10
  • 17
  • You probably want to make a guid. See the answer by Fenton: https://stackoverflow.com/questions/26501688/a-typescript-guid-class – Tyler S. Loeper Jan 07 '19 at 15:15

1 Answers1

4

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();
Srdjan
  • 584
  • 3
  • 6
  • This works for only one time. How do I make it work, so that I can click on a button, that always generates random strings? – Michael Jan 08 '19 at 09:04
  • Add click event to your button and call the makeString() function. I don't know for what you need this function, but now it's uneccessary to return a value... Assign the result to some variable or just console.log it to see if this works for you... – Srdjan Jan 08 '19 at 10:12