Path: csiph.com!news.fcku.it!news.swapon.de!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: srand Date: Sun, 28 Jan 2018 23:09:12 -0800 Organization: None to speak of Lines: 37 Message-ID: References: <5a6eaddd$0$726$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="851279058270e854610aefc6c1465d1e"; logging-data="2278"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+fPU0W/tHn58UX5q219z62" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:Fyg9+Fde8yJ/6mmA9oOBm12H810= sha1:gjxFzTa+afnbENqdfqGweDJF9ng= Xref: csiph.com comp.lang.c:125932 G B writes: > Is there any significant difference between: > > srand((unsigned) time(&t)); > > and > > srand((int) time(null)); > > I have defined: > > time_t t; > > so the program compiles just fine but I want to know whether time null > still gets the current time of the system like time t? > > Just trying to generate some random numbers to test algorithms based on > number of passes and time taken to process the entire function. As Ian pointed out, time(NULL) and time(&t) will return the same result. The only difference is that time(&t) will store the same value in t -- which is not useful unless you're planning to use t later. (The time() function would be simpler if it took no arguments and just returned the value. The design, I think, goes back to very old implementations that couldn't return 32-bit values from functions.) Why do you cast to unsigned in one case and to int in the other? srand() takes an argument of type unsigned int. There's no need to cast it at all; any numeric value you pass will be converted to unsigned int anyway. (It's unsigned int rather than int because it makes the result well defined even if it's out of range.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"