home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!utcsri!newsflash.concordia.ca!IRO.UMontreal.CA!CC.UMontreal.CA!gaumondp
- From: gaumondp@ERE.UMontreal.CA (Gaumond Pierre)
- Subject: Re: Help with generating random numbers
- Message-ID: <1992Nov12.211023.8188@cc.umontreal.ca>
- Sender: news@cc.umontreal.ca (Administration de Cnews)
- Organization: Universite de Montreal
- References: <1992Nov12.041243.5542@desire.wright.edu>
- Date: Thu, 12 Nov 1992 21:10:23 GMT
- Lines: 36
-
- In article <1992Nov12.041243.5542@desire.wright.edu> nseth@desire.wright.edu writes:
- >I need help with generating random numbers. If I use the normail C routines
- >on UNIX for generating random numbers (random, rand, etc.) everytime the
- >program is executed, the sequence of random numers remains the same.
- >
- >How can I fix this so that each time I execute that program, the initial (and
- >thus the following sequence of) random numbers is different? On Turbo C the
- >subroutine randomize() takes care of this. Is there such a routine available on
- >UNIX/ULTRIX?
- >
- >Any help will be greatly appreciated. Thanks a lot.
-
- Usually rand function has an initializer function (srand
- on my system).
-
- use time(2) as the seed for srand (strip most significant
- bits).
-
- For example:
- time(&seed);
- seed = seed % 100000;
- srand(seed);
-
- If you have a time function giving millisecond or mocro seconds. It
- will be better. Usually, my code is suppicient.
-
- >Nitin Seth
- >nseth@cs.wright.edu
- >
-
-
- --
- Pierre Gaumond. gaumondp@ERE.UMontreal.CA
- Services Informatiques, Universite de Montreal.
- C.P. 6128, Succursale "A", Montreal,
- Quebec, Canada. H3C 3J7
-