Generating random password

how to generate random pass word using npm password generator.

note:password should be numeric in nature.

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

1 Like

var generator = require(‘generate-password’);

var password = generator.generate({
length: 10,
numbers:false
});

i am trying to generate numeric password but it generates password containing alphabets

i am using the npm package here to do the task

you are using this to say to not include numbers. Maybe invert this?

see if using the infos on this page you can come up with how to use only numbers: (exclude letters)

I tried but did not come up with anything.Each time i used to get error

You try this?

var password = generator.generate({
  length: 10,
  numbers: true,
  uppercase: false,
  exclude: 'abcdefghijklmnopqrstuvwxyz'
});

Capture

I am getting this instead of numbers

have you tried what you have been suggested? or this is still from your original code?

what’s your code with the suggested changes?

yeah i tried with the suggested code but i could see only difference in cases of the word but could not generate numbers

Yeah I just installed it and tried what I suggested but couldn’t get it to output numbers either.

I tried this package https://www.npmjs.com/package/password-generator

with this line from it’s docs generatePassword(12, false, /\d/)

and it worked fine.

const password = generatePassword(12, false, /\d/);
console.log(password);

If you just want a string of random numbers it should be pretty easy to write a function yourself if you wanted, but this other package I tried does the job without hassle.

var generator = require(‘generate-password’);

var password = generator.generate({
length: 10,
numbers: true,
uppercase:false,
exclude:‘abcdefghijklmnopqrstuvwxyz’
});

console.log(password);

I tried again and i am getting the output as random numbers.

O me too I had a typo oops :slight_smile:

Since technically you are generating only a number, why don’t you do the following, rather than using an external module?

  • use the random number generator of JavaScript to get a number between 0 and 1
  • Multiply it by the max number you expect to have
  • Make it a round number
  • Convert it into string
  • Add the remaining zeros if any are missing

Also note that limiting a password characters to only numbers is extremely bad. You are effectively reducing the number of possible passwords by a ton.

Also, if you are going to store the password in the database, don’t store it in plain text it.
Hash it (possibly with a salt) and saved the hashed passwords.
Also, if you use a different salt per user, save the salts in a different place than the hashed passwords.

Buddy to generate strong passwords, I recommend using a password generator but don’t believe in password generator sites because they usually collect passwords. However, you can try password generators of different brands like Reviewsed.

These brands offer there own created password generators.