14

I need to generate a random MAC address for a project of mine, and I can not get it to work. Below is my current code (that is not working).

function genMAC(){ // Make a new array with all available HEX options. var colours = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"); // Make variable to hold 6 character HEX array partA = new Array(1); partB = new Array(1); partC = new Array(1); partD = new Array(1); partE = new Array(1); partF = new Array(1); mac-address=""; for (i=0;i<2;i++){ // Loop for partA partA[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partB[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partC[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partD[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partE[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partF[i]=colours[Math.round(Math.random()*14)]; } // Returns like "a10bc5". It is likely that you may need to add a "#". mac-address = partA + ":" + partB + ":" + partC + ":" + partD + ":" + partE + ":" + partF; return mac-address; 

}

Ugly. I'm fairly new to JS and I am wondering if there is an easier way to do this that would work.

3
  • There need not be any checking on the MAC address. Basically, all I want is a randomly generated hex string in the form XX:XX:XX:XX:XX:XX Commented Jul 7, 2014 at 23:58
  • 1
    Math.random().toString(16) is a start... Commented Jul 8, 2014 at 0:00
  • @dandavis: …continued to ("0"+Math.floor(Math.random()*256).toString(16)).slice(-2) :-) Commented Jul 8, 2014 at 0:09

3 Answers 3

46
"XX:XX:XX:XX:XX:XX".replace(/X/g, function() { return "0123456789ABCDEF".charAt(Math.floor(Math.random() * 16)) }); 

jsfiddle http://jsfiddle.net/guest271314/qhbC9/

Sign up to request clarification or add additional context in comments.

Comments

13

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.

3 Comments

I really do need to pick up a book on JS. Also, it is not changing each use in the program. That might be a bug in my code though. Yep. it is. I'll fix it later.
It almost certainly is a bug in your code. See, for instance, dstrout.net/html/mac.htm - press the button and the displayed MAC changes every time. If you need help with the part of your code that uses this function, post another question.
This will generate 0 and F about half as frequently as any other individual number.
1

try this Jsfiddle

all i did was to fix the display part :

function genMAC(){ var colours = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"); // Make variable to hold 6 character HEX array var partA = new Array(1); var partB = new Array(1); var partC = new Array(1); var partD = new Array(1); var partE = new Array(1); var partF = new Array(1); var mac_address=""; for (i=0;i<2;i++){ // Loop for partA partA[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partB[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partC[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partD[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partE[i]=colours[Math.round(Math.random()*14)]; } for (i=0;i<2;i++){ // Loop through 6 times, randomising the letter added to the array partF[i]=colours[Math.round(Math.random()*14)]; } // Returns like "a10bc5". It is likely that you may need to add a "#". mac_address = partA[0]+ partA[1] + ":" + partB[0]+ partB[1] + ":" + partC[0]+ partC[1] + ":" + partD[0] + partD[1] + ":" + partE[0] + partE[1] + ":" + partF[0] + partF[1]; alert(mac_address); } 

if you just print/alert the array for example partA it would display like 3,2 and not 32 so you need to concatenate it each index it has.

5 Comments

May I ask why your hexadecimal alphabet has only 14 letters?
@Bergi, probably because I made a typo.
6 times the same typo? (Maybe this type of programming hints at a problem with your code)
@Bergi i only copied the OPs code so basically it's not me. did you just -1 me?
Ah, I see - I had not expected bad code to be reposted without a comment (No, I didn't downvote).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.