home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11809 < prev    next >
Encoding:
Text File  |  1992-07-31  |  1.8 KB  |  57 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!cs.utexas.edu!milano!cactus.org!cheselka
  3. From: cheselka@cactus.org (Michael R. M. Cheselka)
  4. Subject: srand() rand() question
  5. Message-ID: <1992Jul31.113913.22633@cactus.org>
  6. Organization: Capital Area Central Texas UNIX Society, PO Box 9786, Austin, TX 78766-9786
  7. Distribution: usa
  8. Date: Fri, 31 Jul 1992 11:39:13 GMT
  9. Lines: 46
  10.  
  11.   /* What is wrong with this program?  Is it the multiple calls to srand()?
  12.      rand() is not random at all, and soon duplicates it's self in a repeating
  13.      pattern.  Is it illegal to repeatedly call srand()? */
  14.   
  15.   
  16.   
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <ctype.h>
  20.   
  21.   /* The purpose of this program is to create a one megabyte( 1,048,576
  22.      bytes) file containing only print and white space characters.  Usefull to 
  23.      time modem speeds. The characters must be as random as posible, or
  24.      else compression will skew the results. */ 
  25.   
  26.   
  27.   main()
  28. {
  29.   unsigned long count = 1048576;
  30.   unsigned char character;
  31.   unsigned int seed = 67483 ;
  32.   srand( seed);
  33.   while ( count > 0 )
  34.     {
  35.       character = rand();
  36.       if (isprint(character))
  37.         {
  38.           printf("%c", character);
  39.           --count;        
  40.           /*          seed += 3;   Trying for some extra randomness here...
  41.                       srand( seed);    */
  42.         } 
  43.       else if (isspace(character))
  44.         {
  45.           printf("%c", character);
  46.           --count;          
  47.           /*          seed += 5;    And here, doesn't work...
  48.                       srand( seed);    */
  49.         }
  50.     }
  51. }
  52. -- 
  53. cheselka@cactus.org         N5UVV          Michael R. M. Cheselka
  54. os9@gnu.ai.mit.edu        (512)452-9412v ( 24hours answer. mach.)
  55. membership@cactus.org Hangs out on 145.21mhz 400 W. 34th st. #103
  56. {...}!cs.utexas!cactus.org!cheselka     Austin, Texas, 78705-1331
  57.