Skip to content

Instantly share code, notes, and snippets.

@maplambda
Created August 4, 2012 11:41
Show Gist options
  • Save maplambda/3256899 to your computer and use it in GitHub Desktop.
Save maplambda/3256899 to your computer and use it in GitHub Desktop.
(def rand-string (n)
(let c "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
(with (nc 62 s (newstring n) i 0)
(w/infile str "/dev/urandom"
(while (< i n)
(let x (readb str)
(unless (> x 247)
(= (s i) (c (mod x nc)))
(++ i)))))
s)))
def rand_string(n):
c = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
newstring = ""
read_position = 1
with open('/dev/urandom', 'r') as f:
while(len(newstring) < 4):
f.seek(read_position)
ordinal = ord(f.read(1))
if not ordinal > 247:
newstring += c[ordinal % 62]
read_position += 1
return newstring
print (rand_string(4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment