Created
October 7, 2013 00:56
-
-
Save acceptable-security/6861073 to your computer and use it in GitHub Desktop.
My own UUID generator, and comparison.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, math, difflib, uuid | |
def mathround(num): | |
return math.floor(num+0.5) | |
def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"): | |
return ((num == 0) and numerals[0]) or (baseN(num // b, b, numerals).lstrip(numerals[0]) + numerals[num % b]) | |
def addRandomHexDigits(num): | |
builtstring = "" | |
for x in xrange(num): | |
numstr = str(baseN(int(mathround(random.random()*16.0)),16)) | |
builtstring = builtstring + numstr | |
return builtstring | |
def cuuid(): | |
myuuid = "" | |
myuuid = myuuid + addRandomHexDigits(8) + "-" | |
myuuid = myuuid + str(random.randint(0,9)) + addRandomHexDigits(3) + "-" | |
myuuid = myuuid + str(baseN(int(random.random()*4+8),16)) + addRandomHexDigits(3) + "-" | |
myuuid = myuuid + addRandomHexDigits(4) + "-" | |
myuuid = myuuid + addRandomHexDigits(12) | |
return myuuid | |
def similarity(a, b): | |
return difflib.SequenceMatcher(a=a,b=b).ratio() | |
def testUUIDSec(iters): | |
iters -= 2 | |
avg = similarity(cuuid().replace("-",""),cuuid().replace("-","")) | |
for i in xrange(iters): | |
avg = (avg + similarity(cuuid().replace("-",""),cuuid().replace("-","")))/2 | |
print "Custom average similarity: " + str(avg) | |
avg = similarity(str(uuid.uuid4()).replace("-",""),str(uuid.uuid4()).replace("-","")) | |
for i in xrange(iters): | |
avg = (avg + similarity(str(uuid.uuid4()).replace("-",""),str(uuid.uuid4()).replace("-","")))/2 | |
print "Premade average similarity: " + str(avg) | |
def uuidComparisons(iters, iters2): | |
wins = 0 | |
fails = 0 | |
for x in xrange(iters2): | |
iters -= 2 | |
avg1= similarity(cuuid().replace("-",""),cuuid().replace("-","")) | |
for i in xrange(iters): | |
avg1 = (avg1 + similarity(cuuid().replace("-",""),cuuid().replace("-","")))/2 | |
avg2 = similarity(str(uuid.uuid4()).replace("-",""),str(uuid.uuid4()).replace("-","")) | |
for i in xrange(iters): | |
avg2 = (avg2 + similarity(str(uuid.uuid4()).replace("-",""),str(uuid.uuid4()).replace("-","")))/2 | |
if avg1 > avg2: | |
wins += 1 | |
else: | |
fails += 1 | |
print str(wins) + " wins, and " + str(fails) + " fails." | |
print cuuid() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment