home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / question / 13314 < prev    next >
Encoding:
Text File  |  1992-11-12  |  1.6 KB  |  48 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!utcsri!newsflash.concordia.ca!IRO.UMontreal.CA!CC.UMontreal.CA!gaumondp
  3. From: gaumondp@ERE.UMontreal.CA (Gaumond Pierre)
  4. Subject: Re: Help with generating random numbers
  5. Message-ID: <1992Nov12.211023.8188@cc.umontreal.ca>
  6. Sender: news@cc.umontreal.ca (Administration de Cnews)
  7. Organization: Universite de Montreal
  8. References: <1992Nov12.041243.5542@desire.wright.edu>
  9. Date: Thu, 12 Nov 1992 21:10:23 GMT
  10. Lines: 36
  11.  
  12. In article <1992Nov12.041243.5542@desire.wright.edu> nseth@desire.wright.edu writes:
  13. >I need help with generating random numbers. If I use the normail C routines
  14. >on UNIX for generating random numbers (random, rand, etc.) everytime the
  15. >program is executed, the sequence of random numers remains the same. 
  16. >
  17. >How can I fix this so that each time I execute that program, the initial (and
  18. >thus the following sequence of) random numbers is different? On Turbo C the
  19. >subroutine randomize() takes care of this. Is there such a routine available on
  20. >UNIX/ULTRIX?
  21. >
  22. >Any help will be greatly appreciated. Thanks a lot.
  23.  
  24.     Usually rand function has an initializer function (srand
  25.     on my system).
  26.  
  27.     use time(2) as the seed for srand (strip most significant
  28.     bits).
  29.  
  30.     For example:
  31.         time(&seed);
  32.         seed = seed % 100000;
  33.         srand(seed);
  34.  
  35.     If you have a time function giving millisecond or mocro seconds. It
  36.     will be better. Usually, my code is suppicient.
  37.  
  38. >Nitin Seth
  39. >nseth@cs.wright.edu
  40. >
  41.  
  42.  
  43. -- 
  44. Pierre Gaumond.        gaumondp@ERE.UMontreal.CA
  45. Services Informatiques, Universite de Montreal. 
  46. C.P. 6128, Succursale "A", Montreal,            
  47. Quebec, Canada.   H3C 3J7                       
  48.