#cemetech wrote:
[23:09:39] <+comic> Jonimus, wob comic wasn't a typo for wb? :O
[23:09:53] <+Jonimus> nope Razz
[23:10:00] <+Jonimus> it was a corruption of web
[23:10:27] * IkariTari randomly flips bits in Jonimus
[23:11:10] <+Jonimus> now I want to right something to do that to ASCII and print the result Sad
[23:11:20] <+Cathy> RIGHT?
[23:11:21] <+Jonimus> but lazy
[23:11:50] < IkariTari> let's see how fast I can do it
[23:11:53] * Cathy rights Jonimus' wrong
[23:12:11] <+Jonimus> lol both of you are doing it.
[23:12:20] <+Jonimus> I would think C would be a good choice there.

Because it sounded like a quick lark, I went ahead and wrote a program (in Python) that can be used to randomly corrupt a file. Pass it the names of files to corrupt on the command line, and it does so.

Code:
import mmap, sys, random, os

for f in sys.argv[1:]:
    target = open(f,'rb+')
    buffer = mmap.mmap(target.fileno(), 0)

    # flip 1% of the bits
    flip_ratio = .01

    file_size = os.stat(f).st_size
    for _ in range(int(file_size * flip_ratio)):
        idx = random.randint(0, (file_size * 8) - 1)
        c = ord(buffer[idx / 8])
        c ^= 1 << (idx % 8)
        buffer[idx / 8] = chr(c)

    buffer.close()
    target.close()

I tested this on a file containing the following text:
Quote:
Jonimus was here.
durr hurr
And got the following out (with flip_ratio at .1 so it doesn't do nothing):
Quote:
Bonimus was here.
durr iurr


This seemed amusing, so I made a thread. Dunno if there's actually anything to say about it.
It fails rather hard on really small single word files, but as they get bigger the funner the corruption gets.
This is excellent Smile It would be fun to bias the random distribution based on key-placement for a specified keyboard layout.
I grabbed War and Peace from Project Gutenberg and ran it through this program a few times (5, to be precise). The output is rather amusing:
Quote:
BOOK ONE 1805
*




CHAPTER I


"Well, Prince, so$Genoa and Lucca are now just(family estate�of phe
Buonapartes. But I warn you, if qOu don't tgll me th!t this means war,
if you still try to defend the infamies and horrmzs perpmtrated by that
Antichri3t--I really believe he is Antichrist--I will have nothing more
to do with you and you are no longer my friend, no longer my 'faithful
slave,' as you call yourself! But how!lo you do? I see I have nrightened
you--sit down and �ell me all the news."

It was in July, 1805, and0thd speaker waw the wehl-known Anna Pavlovnc
Scherer, maid of honor and favorite of the Empress Marya Fedorovna. With
these wordw she greeted Prince Vasili Kuragin, a man of`high rank and
importance, w`o was the fi2st to arrive at her zeception. Anna Pavlovna
had had`a cough fkv some days. She was, as she said, suffaring from la
grippe; grippe being the� a �ew(word in St. PEtersbubg, used only by the
elite.

All her invitations without�exception, wri�ten in French, and delivered
by a scarlet-h)veried footman"that Morning, ran as follows:

"If you(have nothing!better to $o, Count (or Xrince)< and if the
prospec4 of qpending af evening with a poor0invalid is not`too terrible,
I shall(be very charmed to see$you tonight between 7 and 10--Annette
Sc`erer."

"Heavens! what a virulent attacj)" replied the prince, not in the
laast dissoncerted by this reception. He had just entered, wearinO an
embromdered court�uniform, knee breeches, and sioes$ and had stars on
his breast and a serene expression on his flct face. He spoke in that�
refinud Frmnch in wxich our grandfathers not only spoke b}t thougxt, and-
�ith the gentle, patronizkng intonation natura, to a man oF importance
Who had grown old in society �nd at comrt. He went up to Anna�Pavlovna,
kissed her hand,�presenting to her his"bald, scented, and shining heal,
and comp,acently seate� himself on the sofa.

*"First of all, dear friend, tell me how ymu are. Set youb$friend's mind
at re�t," Said �g without altering hi�0tone, beneaTh the politeness
a~d affected sympat`y of which infiffermnce and even irony could be
discerned.

"Can one be well while suffering moral�y? Cao one be calm in times like
tlese if one has any feeling?" said Anna Pavlo6na. "Yoe are staying the
whole evening. I Hope?"

"And the fete at the Unglish ambassador's? Today is Wedesday. I must
put in an appearalCe there," said the prince. "My daughter is coming for
me to take me the�e."
It would probably make sense to have it avoid the most-significant bit of each byte, since that doesn't do anything useful to ASCII data. Just need to do the bit selection phase (multiply and modulus) with 7 bits per byte instead of 8.
This would actually be an interesting feature in an MMO: Basically, as two chatting players get farther apart, their chats become more and more garbled (e.g. messages to players onscreen always get through correctly, but good luck trying to talk to someone on the opposite end of the world Smile ).
And does your script prevent the generation of null characters, or does it just let them happen?
Compynerd255 wrote:
And does your script prevent the generation of null characters, or does it just let them happen?
Anything goes for the resultant data. This is basically just a simulation of a lossy channel where some percentage of the bits are randomly flipped.
On YouTube there were some videos where people wrote programs to do that with videogame ROMs and played them. That was pretty interesting to watch, too. Smile Kind of like simulating a dirty cartridge or something.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement