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

4
  • here helps you, but not do it Commented Jul 19, 2017 at 5:59
  • I tried math.random function. Commented Jul 19, 2017 at 6:00
  • Check out this link, someone already asked the same question click here Commented Jul 19, 2017 at 6:01
  • @suraj, phihag Please see the alpha numeric format i have given above in the question. Commented Jul 19, 2017 at 6:05

2 Answers 2

1

With coderain library it would be:

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

Disclosure: I'm the author of coderain

Sign up to request clarification or add additional context in comments.

1 Comment

great, exactly what i want. Thanks.
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)); 

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.