Skip to content

Instantly share code, notes, and snippets.

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 antiface/52c662e5b81e34c919a1d5458d048f26 to your computer and use it in GitHub Desktop.
Save antiface/52c662e5b81e34c919a1d5458d048f26 to your computer and use it in GitHub Desktop.
Test random generation when reseeding each iteration
# Requires python-imaging on Ubuntu
import PIL.Image, random, time
class AlexRand(random.Random):
'''This replicates how PHP does randint()'''
def randint(self, a, b):
self.seed()
return super(AlexRand, self).randint(a, b)
image = PIL.Image.new("1",(512,512)) # Creates a 512x512 bitmap
rand_mapping = [
(AlexRand(), "Alex random"),
(random.Random(), "Standard")
]
def time_millis():
return int(round(time.time() * 1000))
for rnd, name in rand_mapping:
start = time_millis();
for y in xrange(512):
for x in xrange(512):
image.putpixel((x,y), rnd.randint(0,1))
image.save("random_"+name+".png")
print "%s took %d ms" % (name, time_millis() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment