Janes
Janes

Reputation: 45

Generate string/characters in JavaScript

Now, I want to set the output generated in constant( first two in letters,last three in numeric). What code should I use? Thanks.

Javascript

function makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}
console.log(makeid());

Upvotes: 0

Views: 1555

Answers (6)

M3talM0nk3y
M3talM0nk3y

Reputation: 1432

You can try the following:

function getRandomId() {
    return String.fromCharCode((Math.random() < 0.5 ? 65 : 97) + Math.floor(Math.random() * 26)) +
        String.fromCharCode((Math.random() < 0.5 ? 65 : 97) + Math.floor(Math.random() * 26)) +
        Math.random().toString(10).substring(2, 5);
}

for (var i = 0; i < 5; i++) {
    console.log(getRandomId());
}

/* Sample Output:
wc257
bR495
Li690
cF973
kf790
*/

Upvotes: 0

joopmicroop
joopmicroop

Reputation: 921

var randomBetween = (min,max)=> Math.floor(max-Math.random()*(max-min));
var randomLetter = ()=> String.fromCharCode(randomBetween(65,90)+(Math.round(Math.random())?32:0));
var makeId = pattern=> (pattern||'xx000').replace(/./g, v=>isNaN(v)?randomLetter():randomBetween(0,9));

console.log(makeId());

Or you can also make your own patterns for example:

makeId('aaaaaa11111');   //returns: lUmKUe07356
makeId('a1a1a1a1a1a1a'); //returns: b2N5U8J2m7r2C

Any matching letter will be replaced with a random one, any number with a random number.

Upvotes: 0

Abhinay Bali
Abhinay Bali

Reputation: 1

Actually you don't need the string "possible" you can use ascii values.

below is the code.

function makeid() {
  var text = "";
var x,y;
  for (var i = 0; i < 2; i++){
  x=(Math.floor(Math.random()*2))?( Math.floor(Math.random() * (26)) + 65):( Math.floor(Math.random() * (26)) + 97)
  text+=String.fromCharCode(x)
  }
  for (var i = 0; i < 3; i++){
 x=String.fromCharCode(Math.floor(Math.random() * (10)) + 48)
  text+=x
  } 
  return text;
}
console.log(makeid()); 

Upvotes: 0

solimanware
solimanware

Reputation: 3051

Here you are -code in ES6-:

You need to separate numbers and characters,

But to generate random unique id you can refer to here

const makeid = () => {
  let id = '';
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  const nums = '0123456789'
  for (let i = 0; i < 2; i++) {
    id += chars[Math.floor(Math.random() * chars.length) + 1]
  }
  for (let i = 0; i < 3; i++) {
    id += nums[Math.floor(Math.random() * nums.length) + 1]
  }
  return id
}

And here's more functional and readable code:

const makeid = () => {
  let id = '';
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  const nums = '0123456789'
  id += generate(2, chars)
  id += generate(3, nums)
  return id
}
const generate = (number, string) => {
  let res = ''
  for (let i = 0; i < number; i++) {
    res += string[Math.floor(Math.random() * string.length) + 1]
  }
  return res
}
console.log(makeid())

Upvotes: 1

Brian Gottier
Brian Gottier

Reputation: 4582

Try this:

function makeid() {
  var text = "";
  var possibleLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var possibleNumbers = "0123456789";

  for (var i = 0; i < 2; i++)
    text += possibleLetters.charAt(Math.floor(Math.random() * possibleLetters.length));
  for (var i = 0; i < 3; i++)
    text += possibleNumbers.charAt(Math.floor(Math.random() * possibleNumbers.length));

  return text;
}

Upvotes: 1

Michael Horn
Michael Horn

Reputation: 4089

You could try separating your 'possible' string into possibleNums and possibleChars

var possibleNums = "0123456789";
var possibleAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

for (var i = 0; i < 2; i++) {
    text += possibleAlpha.charAt(Math.floor(Math.random() * possibleAlpha.length));
}

for (var i = 0; i < 3; i++) {
    text += possibleNums.charAt(Math.floor(Math.random() * possibleNums.length));
}

Upvotes: 3

Related Questions