Share Pastebin
Guest
Public paste!

Mejf

By: a guest | May 13th, 2008 | Syntax: C | Size: 0.83 KB | Hits: 200 | Expires: Never
Copy text to clipboard
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main( int argn, char *argv[] ) {
  5.   int matrix[256][256];
  6.   int total[256];
  7.   int first, next, last;
  8.   int l, m;
  9.  
  10.   first = last = fgetc(stdin);
  11.  
  12.   for (l = 0; l < 256; l++) {
  13.     total[l] = 0;
  14.  
  15.     for (m = 0; m < 256; m++)
  16.       matrix[l][m] = 0;
  17.   }
  18.  
  19.   while ((next = fgetc(stdin)) != EOF) {
  20.     matrix[last][next]++;
  21.     total[last]++;
  22.     last = next;
  23.   }
  24.  
  25.  
  26.   srandom(time(NULL));
  27.  
  28.   last = first;
  29.   putchar(first);
  30.   for (l = 0; l < 10000; l++) {
  31.     if (total[last] <= 0) {
  32.       printf("\n\t**Unexpected ending!\n");
  33.       break;
  34.     }
  35.     int r = random()%total[last] + 1;
  36.     next = 0;
  37.     while (1) {
  38.       r -= matrix[last][next];
  39.       if (r <= 0)
  40.         break;
  41.       next++;
  42.     }
  43.  
  44.     putchar(next);
  45.     last = next;
  46.   }
  47.  
  48.   putchar('\n');
  49. }