|
From: Ioannis V. <iv...@em...> - 2003-03-21 23:17:30
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Ian Hill
> Sent: Friday, March 21, 2003 11:35 AM
> To: dev...@li...
> Subject: RE: [Dev-C++] how to use "srand"
>
>
> The following is a bit of C code that uses the current time as a seed.
> It's an example I have so it may have bits you will need to modify
> (Namespaces and .h files etc.)
> But it does work.
>
>
> /*
> ** Program to demonstrate the use of the rand function
> */
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
> {
> int nCount;
> int nRandom;
>
> /* Seed random number generator with current time */
> srand((unsigned)time(NULL));
There is no need for casting here. The time_t argument (a typedef of a
signed integral type) is implicitly converted to unsigned int.
>
> /* Display 20 random positive integers */
> for(nCount=0; nCount<20; nCount++)
> {
> nRandom=rand();
> printf("%d ", nRandom);
> }
>
> system("PAUSE");
> return 0;
> }
Regards,
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|