home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13283 < prev    next >
Encoding:
Internet Message Format  |  1992-09-08  |  2.5 KB

  1. Path: sparky!uunet!usc!rpi!batcomputer!munnari.oz.au!manuel!sserve!hhcs.gov.au!pihlab
  2. From: pihlab@hhcs.gov.au
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Initializing Random Seeds 
  5. Message-ID: <1992Sep7.104159.354@hhcs.gov.au>
  6. Date: 7 Sep 92 10:41:59 +1000
  7. References: <1992Sep4.173259.12437@maccs.dcss.mcmaster.ca>
  8. Organization: Aust. Dept. Health, Housing and Community Services
  9. Lines: 50
  10.  
  11. In article <1992Sep4.173259.12437@maccs.dcss.mcmaster.ca>, rickan@maccs.dcss.mcmaster.ca (Mark Rickan) writes:
  12. > Please excuse me if this is a FAQ, but is there a ANSI-compliant means
  13. > of initializing a random seed which has a reasonably low probability
  14. > of recurring? At the moment, I'm using srand in conjunction with
  15. > time(&t). Unfortunately, on a fast machine, the time delay is often
  16. > < 1 second, resulting in the production of the same random value.
  17. > I've avoided this problem by use of a time delay, but I'm convinced
  18. > that there must be a more elegant and efficient means of doing this.
  19. > Any suggestions would be appreciated,
  20.  
  21. I don't write in C but it sounds like you want a random number as a seed.
  22.  
  23. One way would be to use a random number as part of your seed determination.
  24.  
  25. Set your MasterSeed1 to some constant value like 123456 or set it based on the
  26. time when they first log on.
  27.  
  28. Everytime that you want a new seed:
  29.  
  30.   1. use your current time sample method to get the time or whatever as a
  31.      SlaveSeed1.
  32.  
  33.   2. use MasterSeed1 to calculate SlaveSeed2.
  34.  
  35.   3. set MasterSeed1 to value of SlaveSeed2 for next cycle.
  36.  
  37.   4. Apply some function to SlaveSeed1 and SlaveSeed2 to give SEED.
  38.  
  39.   5. Use SEED to generate your RANDOM NUMBERS.
  40.      
  41. The function in step 4 could be ADD, MULTIPLY, SUBTRACT, OR, AND, XOR or
  42. whatever works for you.  (Interested in seeing your final implementation)
  43.  
  44. If that still gives you numbers close together or closely related to the
  45. MasterSeed sequence then simply factor in MasterSeed2 and alternate between
  46. using MasterSeed1 and MasterSeed2 etc.
  47.  
  48. -- 
  49.  
  50. Bruce...        pihlab@hhcs.gov.au
  51.                  ^^
  52. *******************************************************************
  53. * Bruce Pihlamae  --  Database Administration                     *
  54. * Commonwealth Department of Health, Housing & Community Services *
  55. * Canberra, Australia                             (W) 06-289-7056 *
  56. *******************************************************************
  57. * These are my own thoughts and opinions, few that I have.        *
  58. *******************************************************************
  59.