Skip to main content
10 of 18
Saved 4 bytes.
user avatar
user avatar

C, 611 615 623 673 707 bytes

Source code:

j,c,p[256][256]={0},r;char*a,*b,*y,Y[612],X[306]="j,c,p[256][256]={0},r;char*a,*b,*y,Y[612],X[306]=\"@\";main(){a=b=X;for(y=Y;*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;*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[612],X[306]="j,c,p[256][256]={0},r;char*a,*b,*y,Y[612],X[306]=\"@\";main(){a=b=X;for(y=Y;*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;*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][2561){p[2];for(c=p[cha;d(*b=*a+++12;r;ifor;if(*a,p[j<0},X;)b=";j]++1)p[j,X[c=*br*b,putcha-6;man(chin(*a+;d(r(r=j<256]=putc],r;*y+=306]=06;for=*b=b,*y=*a+=\";)%r(j;dor=0;j<256][c=b=30;*b=p[256][c][j][c=*a;r()p[6;for=Y;forar(j=*b;for(a+;j=92][306][306;j<2][c]+==0;*y=X;)*b+=0;for(c,Y;},*a+){r()ifor(c=92;j][c][j;for(an(y+=*y+;}";for+++)if(;c)];c=061){a+=r(;*y++)*y=j=j<061256][*y+;*y=*y,c,c][*a+++=p[6;*y,X[6][2]=j];c=*b+)]=r;}";*a=b=j,p[30;ma+)ifo{0;f(*b=Y;d(j;j;d(cha++;chaind(y+)*y=j][*b;for;ifor(j<2;f(j=br+++++++++1256;r(*y++;for;ilear;}

user61383