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

Search for a command to run...

No comments yet. Be the first to comment.
Hi, Hope you are doing fine... Gaurav This side, I hope everything is fine, on your side, and if it is not fine, don't worry it would be : ) This article is regarding AnonMsg which you to give and take Anonymous feedback from anyone. Feedback has al...

Welcome to JavaScript baazi a blog series where we go through common interview questions in JavaScript and understand everything around them. Firstly What is Promise.all() ? So... Promise.all() take an array of promises as input and then return a pro...

In this blog, we are going to build a QR code scanner and generator in Next.js. QR codes are currently powering everything in the world. from payments to id cards, everything has a QR code on it. Here in this small demo we will be building a small de...

Welcome to JavaScript baazi a blog series where we go through common interview questions in JavaScript and understand everything around them. Hmm... So what is the flattening of an array? basically in flattening we reduce the dimension of an array.ba...

Hello 👋 Gaurav This side, I hope everything is fine, on your side, and if it is not fine, don't worry it would be : ) This article is regarding MeowForms which allows you to build custom back endless for free. I have made my small side projects in ...

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.
here is the repo link if you want to deep dive into code