Created
January 19, 2011 12:18
-
-
Save eloraburns/786091 to your computer and use it in GitHub Desktop.
Random bit flipping
This file contains hidden or 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
# Assuming Python >=2.5, <3.0 | |
import os | |
from random import randint | |
num_random_bits = 9 | |
with open('f', 'r+') as f: | |
f.seek(0, os.SEEK_END) | |
length = f.tell()-1 | |
for _ in range(num_random_bits): | |
place = randint(0, length) | |
f.seek(place, os.SEEK_SET) | |
old_byte = ord(f.read(1)) | |
random_bit = 2**randint(0,7) | |
updated_byte = chr(old_byte ^ random_bit) | |
f.seek(place, os.SEEK_SET) | |
f.write(updated_byte) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment