Building a Random Password generator without using the random function in JavaScript.

Building a Random Password generator without using the random function in JavaScript.

hello guys, so I was on my quest to learn javascript and master it, I was just curious I could create my own random password generator without using the random function? well finally I was able to do the show, in this blog, I will tell how I was able to build that in my small project.

So javascript already has a Math. random() which returns a floating-point number a number between 0 and 1 (eg:-0.9343450435664102 ) .

console.log(Math.random());

for generating a random value between 1 to 10 you can use

Math.floor(Math.random() * (10 - 1)) + 1

things get tricky when I decided to create my own function(s) to generate random numbers.

my idea is not perfect it might generate the same numbers sometimes if you are generating numbers too fast but what decided to do was... I thought to generate a random number I should take as many numbers of variables which keep changing according to the user. so I took the sum of date, time, month, seconds, milliseconds, UTC milliseconds even with color depth and pixel depth of the user's device, the chance of getting some of these things the same two times are very very low, later I named this as dateSum. but I also decided to create another variable called timeSum , which would be the sum of hours, min, seconds, milliseconds. now also the craziness does not end no. so I took another variable megaNumber which would be dateSum % timeSum; finally, I will get my number but I can't guarantee that is it a single-digit number, double, or even more.. since I am taking too many variables here... so rest of the code to extract the number at the unit place and return it. (i wanted to use the inbuild function as little as possible here. but please comment the shortest code possible )

getrandom(number) {
    var today = new Date();
    dateSum +=
      today.getFullYear() +
      today.getMonth() +
      today.getDate() +
      today.getSeconds() +
      today.getMilliseconds() +
      today.getUTCMilliseconds() +
      screen.width +
      screen.colorDepth +
      screen.pixelDepth;

    timeSum +=
      today.getHours() +
      today.getMinutes() +
      today.getSeconds() +
      today.getMilliseconds();
    var megaNumber = dateSum % timeSum;
    var numberoflength = megaNumber.toString().length;
    var divide = 1;
    while (numberoflength - 1 > 0) {
      divide *= 10;
      numberoflength--;
    }

    var num = megaNumber % divide;
    if (num > 9) {
      while (num > 9) {
        num = num % 10;
      }
    }
    if (num == 0) {
      num = 1;
    }

    return num;
  }

so now I am able to generate random functions what I need to do is quite easy, now if want to generate random alphabets, symbols what I will do is create a simple string with all the alphabets and symbols I want and then generate a random number and return the alphabet at that index of string.

function randomBigWord() {
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";

  return alphabet[Math.floor((g1.getrandom() * alphabet.length) / 10)];
}

so finally my building blocks are ready..later I just created different big numbers, small numbers, and symbol functions so my random password generator can at least make a strong password. here is what the final small project looks like. if you want to generate your next password using my project here is the link. Screenshot from 2020-12-11 23-40-18.png here is the repo link if you want to deep dive into code

Did you find this article valuable?

Support Gaurav Tewari by becoming a sponsor. Any amount is appreciated!