Skip to main content
You must use 15 instead of 16. The 15th char in hexDigits is 'F', and the 16th is nothing. Without this change the function occasionally generates MacAddresses in the wrong format (ex: xx-xx-x-xx-xx-xx, where an 'x' is missing some place).
Source Link
Nishi
  • 10.8k
  • 3
  • 31
  • 37

Here is a bit more of a "clean" version of your code, which takes the number of loops down to one. On each iteration of the loop, it appends two random characters to the macAddress variable, and if it's not the last iteration, it also appends a colon. Then it returns the generated address.

function genMAC(){ var hexDigits = "0123456789ABCDEF"; var macAddress=macAddress = ""; for (var i = 0; i < 6; i++) { macAddress+=hexDigits.charAt(Math.round(Math.random() * 15)); macAddress+=hexDigits.charAt(Math.round(Math.random() * 15)); if (i != 5) macAddress += ":"; } return macAddress; } 

Aside from being more complicated than necessary, there were two problems with your code. The first was that mac-address is an invalid variable name due to the dash. That caused the code to not work at all. With that fixed, the other problem was that when setting the MAC address variable at the end, you appended arrays to a string. That caused each of the arrays containing two hex digits to be converted to a string, which meant a comma was put between the two digits.

Here is a bit more of a "clean" version of your code, which takes the number of loops down to one. On each iteration of the loop, it appends two random characters to the macAddress variable, and if it's not the last iteration, it also appends a colon. Then it returns the generated address.

function genMAC(){ var hexDigits = "0123456789ABCDEF"; var macAddress= ""; for (var i = 0; i < 6; i++) { macAddress+=hexDigits.charAt(Math.round(Math.random() * 15)); macAddress+=hexDigits.charAt(Math.round(Math.random() * 15)); if (i != 5) macAddress += ":"; } return macAddress; } 

Aside from being more complicated than necessary, there were two problems with your code. The first was that mac-address is an invalid variable name due to the dash. That caused the code to not work at all. With that fixed, the other problem was that when setting the MAC address variable at the end, you appended arrays to a string. That caused each of the arrays containing two hex digits to be converted to a string, which meant a comma was put between the two digits.

Here is a bit more of a "clean" version of your code, which takes the number of loops down to one. On each iteration of the loop, it appends two random characters to the macAddress variable, and if it's not the last iteration, it also appends a colon. Then it returns the generated address.

function genMAC(){ var hexDigits = "0123456789ABCDEF"; var macAddress = ""; for (var i = 0; i < 6; i++) { macAddress+=hexDigits.charAt(Math.round(Math.random() * 15)); macAddress+=hexDigits.charAt(Math.round(Math.random() * 15)); if (i != 5) macAddress += ":"; } return macAddress; } 

Aside from being more complicated than necessary, there were two problems with your code. The first was that mac-address is an invalid variable name due to the dash. That caused the code to not work at all. With that fixed, the other problem was that when setting the MAC address variable at the end, you appended arrays to a string. That caused each of the arrays containing two hex digits to be converted to a string, which meant a comma was put between the two digits.

You must use 15 instead of 16. The 15th char in hexDigits is 'F', and the 16th is nothing. Without this change the function occasionally generates MacAddresses in the wrong format (ex: xx-xx-x-xx-xx-xx, where an 'x' is missing some place).
Source Link

Here is a bit more of a "clean" version of your code, which takes the number of loops down to one. On each iteration of the loop, it appends two random characters to the macAddress variable, and if it's not the last iteration, it also appends a colon. Then it returns the generated address.

function genMAC(){ var hexDigits = "0123456789ABCDEF"; var macAddress="";macAddress= ""; for (var i=0;i i<6;= 0; i < 6; i++) { macAddress+=hexDigits.charAt(Math.round(Math.random()*16 * 15)); macAddress+=hexDigits.charAt(Math.round(Math.random()*16 * 15)); if (i != 5) macAddress+="macAddress += ":"; } return macAddress; } 

Aside from being more complicated than necessary, there were two problems with your code. The first was that mac-address is an invalid variable name due to the dash. That caused the code to not work at all. With that fixed, the other problem was that when setting the MAC address variable at the end, you appended arrays to a string. That caused each of the arrays containing two hex digits to be converted to a string, which meant a comma was put between the two digits.

Here is a bit more of a "clean" version of your code, which takes the number of loops down to one. On each iteration of the loop, it appends two random characters to the macAddress variable, and if it's not the last iteration, it also appends a colon. Then it returns the generated address.

function genMAC(){ var hexDigits = "0123456789ABCDEF"; var macAddress=""; for (var i=0; i<6; i++) { macAddress+=hexDigits.charAt(Math.round(Math.random()*16)); macAddress+=hexDigits.charAt(Math.round(Math.random()*16)); if (i != 5) macAddress+=":"; } return macAddress; } 

Aside from being more complicated than necessary, there were two problems with your code. The first was that mac-address is an invalid variable name due to the dash. That caused the code to not work at all. With that fixed, the other problem was that when setting the MAC address variable at the end, you appended arrays to a string. That caused each of the arrays containing two hex digits to be converted to a string, which meant a comma was put between the two digits.

Here is a bit more of a "clean" version of your code, which takes the number of loops down to one. On each iteration of the loop, it appends two random characters to the macAddress variable, and if it's not the last iteration, it also appends a colon. Then it returns the generated address.

function genMAC(){ var hexDigits = "0123456789ABCDEF"; var macAddress= ""; for (var i = 0; i < 6; i++) { macAddress+=hexDigits.charAt(Math.round(Math.random() * 15)); macAddress+=hexDigits.charAt(Math.round(Math.random() * 15)); if (i != 5) macAddress += ":"; } return macAddress; } 

Aside from being more complicated than necessary, there were two problems with your code. The first was that mac-address is an invalid variable name due to the dash. That caused the code to not work at all. With that fixed, the other problem was that when setting the MAC address variable at the end, you appended arrays to a string. That caused each of the arrays containing two hex digits to be converted to a string, which meant a comma was put between the two digits.

Source Link
Ashley Strout
  • 6.3k
  • 6
  • 28
  • 48

Here is a bit more of a "clean" version of your code, which takes the number of loops down to one. On each iteration of the loop, it appends two random characters to the macAddress variable, and if it's not the last iteration, it also appends a colon. Then it returns the generated address.

function genMAC(){ var hexDigits = "0123456789ABCDEF"; var macAddress=""; for (var i=0; i<6; i++) { macAddress+=hexDigits.charAt(Math.round(Math.random()*16)); macAddress+=hexDigits.charAt(Math.round(Math.random()*16)); if (i != 5) macAddress+=":"; } return macAddress; } 

Aside from being more complicated than necessary, there were two problems with your code. The first was that mac-address is an invalid variable name due to the dash. That caused the code to not work at all. With that fixed, the other problem was that when setting the MAC address variable at the end, you appended arrays to a string. That caused each of the arrays containing two hex digits to be converted to a string, which meant a comma was put between the two digits.