2

Is there a way to generate a random String with 40 random symbols using typescript?

0

2 Answers 2

15

This is taken from a method written by one of our developers. May be this can help. I have modified it for you.

function makeRandom(lengthOfCode: number, possible: string) {
  let text = "";
  for (let i = 0; i < lengthOfCode; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  }
    return text;
}
let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,./;'[]\=-)(*&^%$#@!~`";
const lengthOfCode = 40;
makeRandom(lengthOfCode, possible);
Sign up to request clarification or add additional context in comments.

Comments

5

It's not about TypeScript actually, but JavaScript

You can use plenty of methods i.e.

function randomString(length, chars) {
    var result = '';
    for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
    return result;
}
var rString = randomString(40, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');

or

import some ready-to-use library like https://www.npmjs.com/package/randomstring and use it like

import randomString from 'randomstring';
const result = randomString.generate(40);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.