Monkey Typewriter


/ Published in: C
Save to your folder(s)

"The infinite monkey theorem states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type a given text, such as the complete works of William Shakespeare."

http://en.wikipedia.org/wiki/Infinite_monkey_theorem


Copy this code and paste it in your HTML
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. int randchar(){
  7. if(rand()<RAND_MAX/8)return ' ';
  8. return 'A'+rand()%26;
  9. }
  10.  
  11.  
  12. int main(){
  13. long int z=32768;//# of bytes to output
  14. srand(time(0));
  15. FILE*f=fopen("monkey.txt","wt");
  16. while(z--&&!ferror(f))
  17. putc(randchar(),f);
  18. fclose(f);
  19. system("notepad monkey.txt");//opens file w/ notepad
  20. return 0;
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.