Or a Whale.

HAMLET:
   Do you see yonder cloud that’s almost in shape of a camel?

POLONIUS:
   By the mass, and ’tis like a camel, indeed.

HAMLET:
   Methinks it is like a weasel.

POLONIUS:
   It is backed like a weasel.

HAMLET:
   Or like a whale.

POLONIUS:
   Very like a whale.

Hamlet, Act III

William Shakespeare

#include <string.h>
#include <stdio.h>

#define nb_mutations 20

void swap(char * message, int i, int j)
 {
  char t=message[i];
  message[i]=message[j];
  message[j]=t;
 }

int main()
 {
  char message[]="kalesiksn t iiwesel aihem  t";
  int message_length=strlen(message);
  
  srand(12345);

  for (int i=0;i<nb_mutations;i++)
   {
    swap(message,
         _rand() % message_length,
         _rand() % message_length);
    printf("%s\n",message);
         
   }
 }

*
* *

If it doesn’t work for you, use gcc 4.4+, glibc 2.9+, or if it fails, check it out on ideone.

7 Responses to Or a Whale.

  1. Dan says:

    Anyone been able to get this to work on OSX gcc 4.2.1?

    Fixing the missing stdlib and counter I can get it to compile/run, but I can’t get the correct sequence.

    Something to do with the rand implementation or seeding?

    • Steven Pigeon says:

      Yes, the random sequence is *very important* in this one.

      (or you can solve how it works and make a version for 4.2)

    • Steven Pigeon says:

      For G++ 4.2.4, the string is “ili assaetslm ekkhi ewe tin” (I had a 32 bits machine with 4.2.4 installed)

      • voidah says:

        With gcc 4.4.3 and glib c 2.11.2 it outputs: i l tssakte kwileemnshei ia

        • Steven Pigeon says:

          Then compute the initial string!

        • voidah says:

          Apparently it’s the same initial string that what you got on 4.2.4 (ili assaetslm ekkhi ewe tin) with gcc 4.4.3 and glibc 2.11.2

          Looks like it’s more dependant of the glibc version (implementation of the rand function) than the compiler.

  2. […] minor versions revisions are minor, and essentially harmless, they still can have the effect of breaking your code in unexpected ways, so versions are […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: