Skip to content

Instantly share code, notes, and snippets.

@eloraburns
Created January 19, 2011 12:18
Show Gist options
  • Save eloraburns/786091 to your computer and use it in GitHub Desktop.
Save eloraburns/786091 to your computer and use it in GitHub Desktop.
Random bit flipping
# 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