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


Groups > comp.lang.c > #109908

Re: What am I doing wrong here?

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: What am I doing wrong here?
Date Sat, 13 May 2017 13:58:51 -0700
Organization None to speak of
Lines 26
Message-ID <lninl45v90.fsf@kst-u.example.com> (permalink)
References <of62cn$4ji$3@dont-email.me> <ennoubFn8ntU1@mid.uni-berlin.de>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info mx02.eternal-september.org; posting-host="5ca756bd95c8e8f8c29728d5fd6c16f9"; logging-data="17575"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/UGBsnQLaCDCIWB0gUSzw1"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock sha1:+5SSSL5gFeFLcWD0F1747GGpE7U= sha1:HyY4JXwP7tyisBs358tWowRne98=
Xref csiph.com comp.lang.c:109908

Show key headers only | View raw


jt@toerring.de (Jens Thoms Toerring) writes:
[...]
> There doesn't seen to be any good reason here for using
> sprintf() at all. All you want to do seems to be to out-
> put the results, so your whole loop can bxse shortened to
>
>     srand(time(NULL));
>     for (i = 0; i < atoi(argv[1]); i++)
>         printf("%s, %d, %s\n", names[rand() % namescount],
>                                rates[rand() % ratescount],
>                                jobs[rand()  % jobscount]);

Note that the order of evaluation of the three rand() calls is
unspecified, though it is guaranteed to be called three times.

In the particular case of rand(), that probably doesn't matter,
but it's something to keep in mind.  (And it could be significant
if you call srand() with a fixed seed to get a consistent sequence
of pseudo-random numbers, though it won't be consistent across
implementations.)

-- 
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 | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 00:39 -0400
  Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 01:03 -0400
  Re: What am I doing wrong here? jt@toerring.de (Jens Thoms Toerring) - 2017-05-13 06:55 +0000
    Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 08:59 -0400
      Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 14:15 +0100
      Re: What am I doing wrong here? Gareth Owen <gwowen@gmail.com> - 2017-05-13 14:19 +0100
        Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 11:06 -0400
          Re: What am I doing wrong here? Manfred <noname@invalid.add> - 2017-05-13 17:38 +0200
            Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 13:38 -0400
              Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 19:03 +0100
          Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 16:43 +0100
      Re: What am I doing wrong here? Barry Schwarz <schwarzb@dqel.com> - 2017-05-13 07:44 -0700
        Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 11:25 -0400
          Re: What am I doing wrong here? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-13 19:23 +0100
    Re: What am I doing wrong here? Keith Thompson <kst-u@mib.org> - 2017-05-13 13:58 -0700
      Re: What am I doing wrong here? jt@toerring.de (Jens Thoms Toerring) - 2017-05-13 23:43 +0000
        Re: What am I doing wrong here? Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-14 13:43 -0700
  Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-15 20:18 -0400

csiph-web