0

i am trying to create a program which puts random numbers/letters in a textbox using javascript (in the browser console). I have tried my best to do it but i just couldn't do it.

my best attempt was:

var key = ((nums[Math.floor(Math.random() * nums.length)].toString()) + (Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()));
var key2 = ((Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()));
var key3 = ((Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()) + (Math.floor((Math.random() * 10)).toString()));

But unfortunately it only generates numbers. Does anyone know how to randomize both? Would appreciate if anyone could help me .

Regards, 3hr3nw3rt

1 Answers1

0

You can generate alphanumeric strings using

function alphaNumericString(length) {
    var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
        retVal = "";
    for (var i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }
    return retVal;
}

console.log(alphaNumericString(3))
Corylus
  • 736
  • 5
  • 16