Skip to main content
9 of 18
Saved 8 bytes.
user avatar
user avatar

C, 615 623 673 707 bytes

Source code:

j,c,p[256][256]={0},r;char*a,*b,*y,*Y,X[999]="j,c,p[256][256]={0},r;char*a,*b,*y,*Y,X[999]=\"@\";main(){a=b=X;for(y=Y=X+310;*a-64;)*y++=*a++;for(;*b;*y++=*b++)if(*b==34)*y++=92;for(a++;*a;)*y++=*a++;*y=0;for(a=Y;*a;a++)p[*a][*(a+1)]++;c=*Y;do{putchar(c);for(r=j=0;j<256;j++)r+=p[c][j];r=rand()%r;for(j=0;j<256;j++){r-=p[c][j];if(r<0)break;}c=j;}while(c);}";main(){a=b=X;for(y=Y=X+310;*a-64;)*y++=*a++;for(;*b;*y++=*b++)if(*b==34)*y++=92;for(a++;*a;)*y++=*a++;*y=0;for(a=Y;*a;a++)p[*a][*(a+1)]++;c=*Y;do{putchar(c);for(r=j=0;j<256;j++)r+=p[c][j];r=rand()%r;for(j=0;j<256;j++){r-=p[c][j];if(r<0)break;}c=j;}while(c);}

With newlines and whitespace added for legibility/explanation:

01  j,c,p[256][256]={0},r;
02  char*a,*b,*y,*Y,X[999]="j,c,p[256][256]={0},r;char*a,*b,*y,*Y,X[999]=\"@\";main(){a=b=X;for(y=Y=X+310;*a-64;)*y++=*a++;for(;*b;*y++=*b++)if(*b==34)*y++=92;for(a++;*a;)*y++=*a++;*y=0;for(a=Y;*a;a++)p[*a][*(a+1)]++;c=*Y;do{putchar(c);for(r=j=0;j<256;j++)r+=p[c][j];r=rand()%r;for(j=0;j<256;j++){r-=p[c][j];if(r<0)break;}c=j;}while(c);}";
03  main(){
04      a=b=X;
05      for(y=Y=X+310;*a-64;) *y++=*a++;
06      for(;*b;*y++=*b++) if(*b==34) *y++=92;
07      for(a++;*a;) *y++=*a++; *y=0;
08      for(a=Y;*a;a++) p[*a][*(a+1)]++;
09      c=*Y;
10      do{ putchar(c);
11          for(r=j=0;j<256;j++) r+=p[c][j];
12          r=rand()%r;
13          for(j=0;j<256;j++){
14              r-=p[c][j];
15              if(r<0) break;
16          } c=j;
17      } while(c);
18  }

The variable p[][] will contain the occurrences of each character following another. X contains the entire source, with '@' substituted for the value of X, in quotes. The buffer Y will point start at X[310], one character past the end of the initial string, and will use up 616 bytes. (310 + 616 < 999.)

The for-loops on lines 05, 06, and 07 replace the '@' in X with the content of X, escaping double quotes, and stores it in Y.

Line 08 tallies the occurrences of one character following another. And line 09 starts the Markov model at the first character of the program ('j').

The do/while loop from 10 to 17 first outputs the current state of the Markov model, then finds the next state using a random number within the range of all successive occurrences.

Sample output:

j;r(a]=X;j<0;f(r(c==*b+++++10;d(*y=*y+++);)*a=\"j=r(r+++++=Y=j;*b;f()p[256;r-=0;*y=*b;)*Y;*y+){0;cha++=0;j]+=Y,c];j=0}wha;},X+;j<256;*ar(rar(ar()in(){rar++==0;a-=0;)%r(j;dor={p[9]=Y,r;for(*b;*a++10},r;for+;*y+;*y+++=31){0;*y+;*y+++;*and(*a;r=*a,c][2;}cha]=0;)p[2564)*a+++)p[j,*a;*y++;}

Edit: Cut down 34 bytes to go from 707 to 673 bytes.

Edit: Cut down 50 bytes to go from 673 to 623 bytes.

Edit: Cut down 8 bytes to go from 623 to 615 bytes.

user61383