|
From: Sudhir K K. <ks...@ly...> - 2003-06-18 22:38:53
|
> >Greets, Artificial Stupidity, I'm Real Happy (to meet you). > >> HI, does anyone know a good way to use rnd() to generate non-repeating >random numbers - in the area of about a thousand. > > rand()%1000; // Gives a number from 0 - 999 > >Also, at the beginning of the program, do > > srand(time(NULL)); > >This is to initialize (seed) the random number so that you get different >sequences everytime you run the program. May be it is a little better to use the time in micro/milliseconds in some form as the seed instead of time (NULL) which returns seconds. This will make sure that two instances of the program started almost the same time will not get the same random number sequence. In SVR4/BSD, gettimeofday will do the trick but not sure what is the POSIX way of getting microseconds. Sudhir > >HTH. >________________________________________ > >Abhijit Shylanath >http://mudeth.tripod.com/ > > > >------------------------------------------------------- >This SF.Net email is sponsored by: INetU >Attention Web Developers & Consultants: Become An INetU Hosting Partner. >Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission! >INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php >_______________________________________________ >Dev-cpp-users mailing list >Dev...@li... >TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > ____________________________________________________________ Get advanced SPAM filtering on Webmail or POP Mail ... Get Lycos Mail! http://login.mail.lycos.com/r/referral?aid=27005 |
|
From: Ioannis V. <iv...@em...> - 2003-06-19 00:00:21
|
> -----Original Message----- > From: Ioannis Vranos [mailto:iv...@em...] > Sent: Thursday, June 19, 2003 2:59 AM > To: 'dev...@li...' > Subject: RE: [Dev-C++] rand() > > > time() is not returning seconds it is returning time_t which > can be whatever the implementation wants. Two subsequent > calls of time() cannot return the same value since it > represents the calendar time (day. month, year, hour, > minutes, seconds...). ... with the exception of the case of error where (time_t)-1 is returned. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Ioannis V. <iv...@em...> - 2003-06-18 23:58:45
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Sudhir K KRISHNAWARRIER > Sent: Thursday, June 19, 2003 1:10 AM > To: dev...@li... > Subject: RE: [Dev-C++] rand() >=20 > May be it is a little better to use the time in=20 > micro/milliseconds in some form as the seed instead of time=20 > (NULL) which returns seconds. This will make sure that two=20 > instances of the program started almost the same time will=20 > not get the same random number sequence. In SVR4/BSD,=20 > gettimeofday will do the trick but not sure what is the POSIX=20 > way of getting microseconds. time() is not returning seconds it is returning time_t which can be = whatever the implementation wants. Two subsequent calls of time() cannot return = the same value since it represents the calendar time (day. month, year, = hour, minutes, seconds...). Ioannis Vranos =20 * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Per W. <pw...@ia...> - 2003-06-19 00:49:24
|
I'm not so sure about your definition of time(). time_t is a type - might be int, long, struct xx, ... depending on implementation and processor register width etc. However, without checking the latest c++ standard, time() returns time in seconds since epoch (seconds since 1969). With a resolution of one second, it is definitely possible to call time() multiple times and get the same return value. /Per W Yes, two calls to time() within the same second will retu On Thu, 19 Jun 2003, Ioannis Vranos wrote: > > -----Original Message----- > > From: dev...@li... > > [mailto:dev...@li...] On Behalf > > Of Sudhir K KRISHNAWARRIER > > Sent: Thursday, June 19, 2003 1:10 AM > > To: dev...@li... > > Subject: RE: [Dev-C++] rand() > > > > May be it is a little better to use the time in > > micro/milliseconds in some form as the seed instead of time > > (NULL) which returns seconds. This will make sure that two > > instances of the program started almost the same time will > > not get the same random number sequence. In SVR4/BSD, > > gettimeofday will do the trick but not sure what is the POSIX > > way of getting microseconds. > > > time() is not returning seconds it is returning time_t which can be whatever > the implementation wants. Two subsequent calls of time() cannot return the > same value since it represents the calendar time (day. month, year, hour, > minutes, seconds...). > > > > > > Ioannis Vranos > > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL 1: http://run.to/noicys > * Alternative URL 2: http://www.noicys.cjb.net > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: INetU > Attention Web Developers & Consultants: Become An INetU Hosting Partner. > Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission! > INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ioannis V. <iv...@em...> - 2003-06-19 13:18:50
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Per Westermark > Sent: Thursday, June 19, 2003 3:51 AM > To: Ioannis Vranos > Cc: dev...@li... > Subject: RE: [Dev-C++] rand() > > > I'm not so sure about your definition of time(). > > time_t is a type - might be int, long, struct xx, ... depending on > implementation and processor register width etc. It's an intergal type. > However, without checking the latest c++ standard, time() > returns time in > seconds since epoch (seconds since 1969). In UNIXoids (probably in POSIX standard) yes, and if I recall correctly it is since january of 1970. However this is not required by the standard and if I recall correctly againn (which doesn't matter) this is not the case for Windows. > With a resolution of one second, it is definitely possible to > call time() > multiple times and get the same return value. Yes you are right. Well in such case (for subsequent calls in less than time_t) one can use a combination of time() and clock() etc). However one could make a thread to wait for a couple of time_ts if he has such considerations. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Daniel K. O. <dan...@ya...> - 2003-06-19 15:22:35
|
Maybe you all would like to give a read on this: comp.lang.c FAQ http://www.eskimo.com/~scs/C-faq/top.html The questions 13.15 ~ 13.20 are about random numbers. Daniel K. O. |