skip to Main Content

I am trying to make an random password generator in node.js but it does not work.

function betterRandom(min, max) {
    if (max == null){ //checks if there in no max
        max = min;
        min = 0;
    }
    return Math.round((Math.random() * (max - min) + min));
}
function randomPassword(length) {
    let chars = [
        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
        ['a','b','c','d','e','f','g','h','i','j'],
        ['k','l','m','n','o','p','q','r','s','t'],
        ['u','v','w','x','y','z','A','B','C','D'],
        ['E','F','G','H','I','J','K','L','M','N'],
        ['O','P','Q','R','S','T','U','V','W','X'],
        ['Y','Z','!','@','#','$','%','?','=',';']];
    let result = "";
    for (let i = 0; i < length; i++) {
        let x = betterRandom(0, 9);
        let y = betterRandom(0, 6);
        console.log(`${x}, ${y}`);

        result += chars [x][y]; //adds random character from chars to results
        result += ' ';
        console.log(chars[x] [y]);
    }
    console.log(result);

}

randomPassword(5);

I tried to fix it and it didn’t work and I thought it would spit out at the end a random password if a specified length is passed.

4

Answers


  1. It looks like there’s a small issue in your ‘betterRandom’ function. It should use ‘Math.floor’ instead of ‘Math.round’

    Login or Signup to reply.
  2. function betterRandom(min, max) {
        if (max == null){ //checks if there in no max
            max = min;
            min = 0;
        }
        return Math.round((Math.random() * (max - min) + min));
    }
    function randomPassword(length) {
        let chars = [
            ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
            ['a','b','c','d','e','f','g','h','i','j'],
            ['k','l','m','n','o','p','q','r','s','t'],
            ['u','v','w','x','y','z','A','B','C','D'],
            ['E','F','G','H','I','J','K','L','M','N'],
            ['O','P','Q','R','S','T','U','V','W','X'],
            ['Y','Z','!','@','#','$','%','?','=',';']];
        let result = "";
        for (let i = 0; i < length; i++) {
            let x = betterRandom(0, 6);
            let y = betterRandom(0, 9);
            //console.log(`${x},, ${y}`);
    
            result += chars [x][y]; //adds random character from chars to results
            result += ' ';
            //console.log(result); //chars [x][y]
        }
        console.log(result);
    
    }
    
    randomPassword(5);

    If you don’t want space between generated password then remove this line of code.

    result += ' ';

    Your code is working,check it.

    You just need to change this two lines ,

    let x = betterRandom(0, 6);
    let y = betterRandom(0, 9);
    
    Login or Signup to reply.
  3. change
    result += chars [x][y]
    into
    result += chars [y][x]

    EDIT: also change the console.log line to : console.log(chars[y][x]);

    and it should work 😉

    Login or Signup to reply.
  4. Your methodology is overly complex and slightly flawed.

    1. You should never use Math.random() for anything security related, you want one of the random functions from the crypto module.
    2. Using a multi-dimensional array increases the complexity when all you need to do is generate a random integer that corresponds to a valid ascii character.

    Assuming you want to allow any ascii character, that integer range is [ 33, 126 ]:

    $ ascii -d
        0 NUL    16 DLE    32      48 0    64 @    80 P    96 `   112 p 
        1 SOH    17 DC1    33 !    49 1    65 A    81 Q    97 a   113 q 
        2 STX    18 DC2    34 "    50 2    66 B    82 R    98 b   114 r 
        3 ETX    19 DC3    35 #    51 3    67 C    83 S    99 c   115 s 
        4 EOT    20 DC4    36 $    52 4    68 D    84 T   100 d   116 t 
        5 ENQ    21 NAK    37 %    53 5    69 E    85 U   101 e   117 u 
        6 ACK    22 SYN    38 &    54 6    70 F    86 V   102 f   118 v 
        7 BEL    23 ETB    39 '    55 7    71 G    87 W   103 g   119 w 
        8 BS     24 CAN    40 (    56 8    72 H    88 X   104 h   120 x 
        9 HT     25 EM     41 )    57 9    73 I    89 Y   105 i   121 y 
       10 LF     26 SUB    42 *    58 :    74 J    90 Z   106 j   122 z 
       11 VT     27 ESC    43 +    59 ;    75 K    91 [   107 k   123 { 
       12 FF     28 FS     44 ,    60 <    76 L    92    108 l   124 | 
       13 CR     29 GS     45 -    61 =    77 M    93 ]   109 m   125 } 
       14 SO     30 RS     46 .    62 >    78 N    94 ^   110 n   126 ~ 
       15 SI     31 US     47 /    63 ?    79 O    95 _   111 o   127 DEL
    

    Then using the randomInt() function in the native crypto module you can easily accomplish what you’re trying to do:

    const crypto = require('node:crypto');
    
    const LEN = 10,
          ASCII_MAX = 126,
          ASCII_MIN = 33;
    
    let buff = Buffer.alloc(LEN);
    
    for(let idx=0; idx<LEN; idx++) {
      let ascii = crypto.randomInt(ASCII_MIN, ASCII_MAX+1);
      buff.writeUInt8(ascii, idx);
    }
    
    console.log(buff.toString());
    

    Prints:

    $ node foo.js 
    {2u4c;iVG
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search