-
-
Save antiface/52c662e5b81e34c919a1d5458d048f26 to your computer and use it in GitHub Desktop.
Test random generation when reseeding each iteration
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
# 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