-5

I am new here and new to coding with python. I recently started an online coarse and wanted to see if there was an easier way that I could have created a 12 digit random hex list. I wanted each individual bit to be a random number or letter either lowercase or uppercase. Based on my rookie research this was the best I could find and created my own code.. so could I have done this a lot simpler and efficient?

#!/user/bin/python
import string
import random 

r1 = random.choice(string.hexdigits)
r2 = random.choice(string.hexdigits) 
r3 = random.choice(string.hexdigits)  
r4 = random.choice(string.hexdigits) 
r5 = random.choice(string.hexdigits)
r6 = random.choice(string.hexdigits)
r7 = random.choice(string.hexdigits)
r8 = random.choice(string.hexdigits)
r9 = random.choice(string.hexdigits)
r10 = random.choice(string.hexdigits)
r11 = random.choice(string.hexdigits)
r12 = random.choice(string.hexdigits)

randnumb = r1+r2+":"+r3+r4+":"+r5+r6+":"+r7+r8+":"+r9+r10+":"+r11+r12

print(randnumb)
Jbravo
  • 43
  • 6
  • 3
    Hi there, welcome to StackOverflow. For your information, [images of code are highly discourages](http://idownvotedbecau.se/imageofcode). – TrebledJ Dec 02 '18 at 09:55
  • 1
    Please post your code as text, not images. Also, if your code is already working and you only want to improve it, your question is probably off-topic for SO, and might be more relevant on Code Review. – Thierry Lathuille Dec 02 '18 at 09:55

1 Answers1

1

You can use a loop with str.join():

from random import choice
from string import hexdigits

print(":".join(choice(hexdigits) + choice(hexdigits) for _ in range(6)))
# 47:8b:FA:71:90:ea

Which combines two random hex digits 6 times, equivalent to your 12 separate calls.

Or as @Jon Clements suggests in the comments:

':'.join(format(choice(range(256)), 'x') for _ in range(6))

Which uses Hex format 'x' with format() across all 256 ASCII characters.

Additionally, to be super safe you can pad format to 2 characters with 02x/02X:

format(choice(range(256)), '02x') # lowercase padding
format(choice(range(256)), '02X') # uppercase padding

x is lower-case hexadecimal and X is upper-case hexidecimal. It's good to be consistent here to avoid strange lowercase and uppercase mixings. One example of this consistency is MAC addresses, where the hexidecimal strings are only in lowercase.

You can read more about this and its variations in the Format Specification Mini-Language.

Another thing you can do is replace choice(range(256)) with random.randrange(256), avoiding the need to use range().

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • Or... `':'.join(format(choice(range(256)), 'x') for _ in range(6))`... – Jon Clements Dec 02 '18 at 10:19
  • @JonClements Very nice. I added your answer in. – RoadRunner Dec 02 '18 at 10:27
  • @RoadRunner Hey thanks for the help. I'm pretty new to coding in general and I can kinda understand the code you suggested, but I don't know how to run it. I just typed the 3 lines in and nothing happened. what else did you put in if its not too much to ask. or what was the full code – Jbravo Dec 02 '18 at 10:34
  • @Jbravo The first snipped of code I wrote in the interpreter, I put nothing else in. If you paste the first example in, it should work. I'm not sure what your python enviorment is, but I run scripts with `python ` in the command line. – RoadRunner Dec 02 '18 at 11:07
  • @RoadRunner ummm... just thought it should be `02x` for the format string to ensure it's always padded to 2 characters when anything less than 16 is chosen as the random value... and wouldn't hurt to use `random.randrange(256)` instead of `choice(range(256))`... – Jon Clements Dec 02 '18 at 13:44
  • 1
    Yup... `x` is for lower case and `X` is for upper case hex digits... keeps it consistent, otherwise since `string.hexdigits` is `'0123456789abcdefABCDEF'` you can end up with an odd looking (but still valid) string, eg: `aA:Fe` kind of thing when choosing randomly from it. – Jon Clements Dec 02 '18 at 14:01
  • @JonClements Thanks for the feedback, those are some great points. I guess I've learnt something new today. – RoadRunner Dec 02 '18 at 14:06
  • @RoadRunner Thank you guys for the feedback. Its really useful information! Hopefully I can get good enough and understand coding to help others soon. – Jbravo Dec 02 '18 at 19:35