0

I need a method that generates an alphanumeric string that starts with a random non numeric character (a,b,c...z).

This is what I've done till now:

const makeRandomString = (len) => {
  return [...Array(len)].map(() => Math.random().toString(36)[2]).join("");
};

How can I add in an elegant way the random non numeric character in front of the alphanumeric string?

tazmin32w
  • 127
  • 10

4 Answers4

1

Another one approach:

const makeRandomString = (len = 5) => {
    const getRandomChar = (...params) => {
        const symbols = params.join('');
        return  symbols[Math.floor(Math.random() * symbols.length)];
    };
    
    const alfas = 'abcdefghijklmnopqrstuvwxyz';
    const nums = '0123456789';
    
    const first = getRandomChar(alfas);
    const rest = [...Array(len - 1)].map(() => getRandomChar(alfas, nums));

    return [first, ...rest].join('');
};

console.log(makeRandomString());
.as-console-wrapper { max-height: 100% !important; top: 0 }
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
0

It is generally not recommended to use Math.random().toString(), to have more control, try one of many custom function like this one:

function makeid(length, includeDigits = true) {
  var result = '';

  var characters = 'abcdefghijklmnopqrstuvwxyz';
  if (includeDigits) characters += '0123456789';

  var charactersLength = characters.length;
  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}

function makeRandomString(len = 8) {
  return makeid(1, false) + makeid(len - 1);
}

console.log(makeRandomString());
Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
  • This is great but if `includeDigits` is `true` the string generated has a numeric character at the start which is not what the question wants. – Pratik Wadekar Jul 26 '22 at 08:06
  • @PratikWadekar Yes, you'd have to call `makeRandomString(1, false) + makeRandomString(7)`, better to create a second function for it. – Idrizi.A Jul 26 '22 at 08:10
0

What you can do is generate the first letter separatly from the rest of the string.

This can be done by taking a random letter from a string for example.

Exemple

const CHARACTERS='abcdefghijklmnopqrstuvwxyz'


const makeRandomString = (len) => {
  const letter = CHARACTERS[Math.floor(Math.random()*CHARACTERS.length)];
  return letter + [...Array(len - 1)].map(() => Math.random().toString(36)[2]).join("");
};

console.log(makeRandomString(10))
RenaudC5
  • 3,553
  • 1
  • 11
  • 29
  • The string returned by the `Math.random().toString(36)` is finite, so the maximum length of the random string is very limited here. – Ivar Jul 26 '22 at 08:17
  • I think you shouldn't pass `len` as a parameter since it's misleading, e.g. `makeRandomString(16)` will return a string with 10-12 characters – Idrizi.A Jul 26 '22 at 08:18
  • Oh yeah you're right, haven't thought about this. – RenaudC5 Jul 26 '22 at 08:29
0

Just seperate out the possible characters for each position (first and rest).

function randomString(length) {
    var alpha = "abcdefghijklmnopqrstuvwxyz";
    var alnum = alpha + "0123456789";
    const randomChar = possible => possible.charAt(Math.floor(Math.random() * possible.length))
    return Array(length).fill(0).map((_, index) => randomChar(index ? alnum : alpha)).join('')
}

console.log(randomString(10));
Dave Meehan
  • 3,133
  • 1
  • 17
  • 24