Generate a Random MAC Address in Python

If you’d like to learn more about programming, contact me for a one-on-one lesson.

1
2
3
4
5
6
7
8
9
10
import random
 
def randomMacAddress():
   """Returns a completely random Mac Address"""
   mac = [0x00, 0x16, 0x3e, random.randint(0x00, 0x7f), \
      random.randint(0x00, 0xff), random.randint(0x00, 0xff)]
   return ':'.join(map(lambda x: "%02x" % x, mac))
 
if __name__ == '__main__':
   print randomMacAddress()

12 FEEDBACKS

  1. Name says:

    I find map(lambda... hard to read.

    I prefer ':'.join('%02x' % x for x in mac)

    /useless critique

  2. kennethreitz says:

    Interesting… I've always been a bit partial to map i suppose :)

  3. The backslash is not necessary. The space after the opening bracket goes against the style recommendations of PEP-8 and is inconsistent with the lack of a space in front of the closing bracket.

    Some people would prefer ':'.join('%02x' % x for x in mac) instead of map + lambda.

  4. BTW an interesting commenting interface. You're encouraged to type your comment before reading what others already posted — in fact, due to the size of my screen, I didn't even notice there were already some comments.

  5. kennethreitz says:

    I have the option to put the comment box at both the top and the bottom. sometimes the comments get rather large, and rather than pagination, i prefer them all to be listed on the page. When this is the case, for usability (everyone just wants to put in their two cents) I like to keep it up top. :)

  6. kennethreitz says:

    The backslash is there for the sole purpose of keeping the code a certain width for my aesthetics :)

    Thanks for the braket+space comment. I'll fix that.

  7. The backslash is not necessary. The space after the opening bracket goes against the style recommendations of PEP-8 and is inconsistent with the lack of a space in front of the closing bracket.

    Some people would prefer ':'.join('%02x' % x for x in mac) instead of map + lambda.

  8. BTW an interesting commenting interface. You're encouraged to type your comment before reading what others already posted — in fact, due to the size of my screen, I didn't even notice there were already some comments.

  9. I have the option to put the comment box at both the top and the bottom. sometimes the comments get rather large, and rather than pagination, i prefer them all to be listed on the page. When this is the case, for usability (everyone just wants to put in their two cents) I like to keep it up top. :)

  10. The backslash is there for the sole purpose of keeping the code a certain width for my aesthetics :)

    Thanks for the braket+space comment. I'll fix that.

  11. <script type=”text/javascript” charset=”utf-8″>

    </script>

  12. <ul id=”mylist”>
    <li class=”listitems”>

    my name is


    </li>
    <li class=”listitems”>

    billy joe


    </li>
    <li class=”listitems”>

    i'm an idiot


    </li>
    </ul>

LEAVE A COMMENT

Additional comments powered by BackType