Skip to content

Instantly share code, notes, and snippets.

@gockxml
Created August 27, 2012 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gockxml/3488007 to your computer and use it in GitHub Desktop.
Save gockxml/3488007 to your computer and use it in GitHub Desktop.
generate random-like id
import math
def generate(id, align = 8):
a = bin(id)[2:]
bin_align = int(math.log(10 ** align, 2))
fill_a = a.rjust(bin_align, '0')
left = fill_a[: bin_align / 2]
right = fill_a[bin_align / 2:]
temp = [int(right[-1])]
for i in right[::-1][1:]:
temp.append(temp[-1] ^ int(i))
print "fill_a : ", fill_a
print "right part : ", temp
temp2 = []
for i in range(0, len(left)):
temp2.append(int(left[i]) ^ int(right[i]))
print "left part : ", temp2
result = int(''.join(map(lambda s:str(s), temp + temp2)), 2)
print "result : ", result
return result
generate(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment