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?

  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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: