2

I want to generate a random alpha numeric number in the format of 6 alphabets 3 numerics and 3 alphabets as given below. Thanks.

example: aiemsd159pku

Kannan K
  • 4,411
  • 1
  • 11
  • 25

2 Answers2

1

With coderain library it would be:

var cr = new CodeRain("aaaaaa999aaa");
var code = cr.next();

Disclosure: I'm the author of coderain

Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
0

You can provide an array of the characters to use for the resulting string, use String.prototype.repeat() to create a string having N .length of space character " ", String.prototype.replace() to replace space character with character from provided string, array or other object.

const randomStringSequence = (
                              keys = [
                                "abcdefghijklmnopqrstuvwxyz"
                                , "0123456789"
                              ]
                             , props = [
                                // `.length` of sequence, `keys` to use
                                 [6, keys[0]],
                                 [3, keys[1]], 
                                 [3, keys[0]]
                               ]
                             ) =>
                               props.map(([key, prop]) =>
                                 " ".repeat(key).replace(/./g, () =>
                                   prop.charAt(
                                     Math.floor(Math.random() * prop.length))
                                   )
                               ).join("");

// call with default parameters
console.log(randomStringSequence()); 

let keys = ["x0y1z9", "_*-?!~"];
let props = [[3, keys[1]], [3, keys[0]], [3, keys[1]]];

// pass `keys` and `props` as parameters
console.log(randomStringSequence(keys, props)); 
guest271314
  • 1
  • 15
  • 104
  • 177