Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Need help with python

Name: shink !xi8/JKFwzo 2012-07-11 4:08

hey /prog/
Need help with python
I am randomly generating all possible 20 character long strings consisting of 1234567890abcdef, and printing them to a file.
I generate the strings using:

CHARS="abcdef1234567890"
def rand_string(string_length):
        output = ''            
       
        for i in xrange(string_length):
                output += random.choice(CHARS)


Now i need to print rand_string(20) over and over again to an output file, but checking said file for that string, and only printing it if it isn't there yet.

Any thoughts?

Name: Anonymous 2012-07-11 12:47

>>1

printed = set()
for _ in xrange(strings_to_print):
    s = rand_string(string_length)
    if s not in printed:
        print s
        printed.add(s)

But the chances to get a collision with 80 bit strings are pretty much negligible, so maybe it's not worth the effort.

Also, use ''.join(random.choice(CHARS) for _ in xrange(string_length)) instead of your silly loop.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List