tom
tom

Reputation: 55

python intelligent hexadecimal numbers generator

I want to be able to generate 12 character long chain, of hexadecimal, BUT with no more than 2 identical numbers duplicate in the chain: 00 and not 000 Because, I know how to generate ALL possibilites, including 00000000000 to FFFFFFFFFFF, but I know that I won't use all those values, and because the size of the file generated with ALL possibilities is many GB long, I want to reduce the size by avoiding the not useful generated chains.

So my goal is to have results like 00A300BF8911 and not like 000300BF8911

Could you please help me to do so? Many thanks in advance!

Upvotes: -3

Views: 1319

Answers (6)

CodeNStuff
CodeNStuff

Reputation: 314

You could use a generator iterating a window over the strings your current implementation yields. Sth. like (hex_str[i:i + 3] for i in range(len(hex_str) - window_size + 1)) Using len and set you could count the number of different characters in the slice. Although in your example it might be easier to just compare all 3 characters.

Upvotes: 0

Zev
Zev

Reputation: 3491

if you picked the same one twice, remove it from the choices for a round:

import random
hex_digits = set('0123456789ABCDEF')

result = ""
pick_from = hex_digits
for digit in range(12):
    cur_digit = random.sample(hex_digits, 1)[0]
    result += cur_digit
    if result[-1] == cur_digit:
        pick_from = hex_digits - set(cur_digit)
    else:
        pick_from = hex_digits

print(result)

Since the title mentions generators. Here's the above as a generator:

import random
hex_digits = set('0123456789ABCDEF')

def hexGen():
    while True:
        result = ""
        pick_from = hex_digits
        for digit in range(12):
            cur_digit = random.sample(hex_digits, 1)[0]
            result += cur_digit
            if result[-1] == cur_digit:
                pick_from = hex_digits - set(cur_digit)
            else:
                pick_from = hex_digits
        yield result

my_hex_gen = hexGen()
counter = 0

for result in my_hex_gen:
    print(result)
    counter += 1
    if counter > 10:
        break

Results:

1ECC6A83EB14
D0897DE15E81
9C3E9028B0DE
CE74A2674AF0
9ECBD32C003D
0DF2E5DAC0FB
31C48E691C96
F33AAC2C2052
CD4CEDADD54D
40A329FF6E25
5F5D71F823A4 

You could also change the while true loop to only produce a certain number of these based on a number passed into the function.

Upvotes: 1

Kevin
Kevin

Reputation: 76184

I interpret this question as, "I want to construct a rainbow table by iterating through all strings that have the following qualities. The string has a length of 12, contains only the characters 0-9 and A-F, and it never has the same character appearing three times in a row."

def iter_all_strings_without_triplicates(size, last_two_digits = (None, None)):
    a,b = last_two_digits
    if size == 0:
        yield ""
    else:
        for c in "0123456789ABCDEF":
            if a == b == c:
                continue
            else:
                for rest in iter_all_strings_without_triplicates(size-1, (b,c)):
                    yield c + rest

for s in iter_all_strings_without_triplicates(12):
    print(s)

Result:

001001001001
001001001002
001001001003
001001001004
001001001005
001001001006
001001001007
001001001008
001001001009
00100100100A
00100100100B
00100100100C
00100100100D
00100100100E
00100100100F
001001001010
001001001011
...

Note that there will be several hundred terabytes' worth of values outputted, so you aren't saving much room compared to just saving every single string, triplicates or not.

Upvotes: 1

Reblochon Masque
Reblochon Masque

Reputation: 36652

You could extract a random sequence from a list of twice each hexadecimal digits:

digits = list('1234567890ABCDEF') * 2
random.shuffle(digits)
hex_number = ''.join(digits[:12])

If you wanted to allow shorter sequences, you could randomize that too, and left fill the blanks with zeros.

import random

digits = list('1234567890ABCDEF') * 2
random.shuffle(digits)
num_digits = random.randrange(3, 13)
hex_number = ''.join(['0'] * (12-num_digits)) + ''.join(digits[:num_digits])

print(hex_number)

Upvotes: 0

lenik
lenik

Reputation: 23498

import string, random

source = string.hexdigits[:16]
result = ''
while len(result) < 12 :
    idx = random.randint(0,len(source))
    if len(result) < 3 or result[-1] != result[-2] or result[-1] != source[idx] :
        result += source[idx]

Upvotes: 0

COLAMAroro
COLAMAroro

Reputation: 21

You can create an array from 0 to 255, and use random.sample with your list to get your list

Upvotes: -1

Related Questions