Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #125932

Re: srand

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: srand
Date 2018-01-28 23:09 -0800
Organization None to speak of
Message-ID <ln8tch9m5j.fsf@kst-u.example.com> (permalink)
References <5a6eaddd$0$726$14726298@news.sunsite.dk>

Show all headers | View raw


G B <gb@gb.com> 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  <http://www.ghoti.net/~kst>
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"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

srand G B <gb@gb.com> - 2018-01-29 05:15 +0000
  Re: srand Ian Collins <ian-news@hotmail.com> - 2018-01-29 18:27 +1300
  Re: srand Keith Thompson <kst-u@mib.org> - 2018-01-28 23:09 -0800
    Re: srand Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-01-29 11:32 +0000
  Re: srand Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-01-29 08:46 -0700
  Re: srand Real Troll <Real.Troll@Trolls.com> - 2018-01-29 17:59 -0400
    Re: srand bartc <bc@freeuk.com> - 2018-01-29 22:07 +0000
    Re: srand Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-02-05 12:54 +0000

csiph-web