time()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    time()

    I have no idea what to do with time () or another function to change the
    time_t data type to int to get numbers say from 0-20 that are ints. time()
    reports a huge number of seconds and when using rand() truely produce a fake
    randomness. I want smaller numbers. We have functions like strtod and strtol
    to change chars into doubles or longs why not change time_t to int?

    Bill


  • Thad Smith

    #2
    Re: time()

    Bill Cunningham wrote:
    I have no idea what to do with time () or another function to change the
    time_t data type to int to get numbers say from 0-20 that are ints.
    What units do you want? gmtime() will convert time_t into struct tm
    fields. Standard C doesn't define the encoding of time_t values.
    time()
    reports a huge number of seconds
    maybe
    and when using rand() truely produce a fake
    randomness.
    A pseudo random numbergenerator does not generate true random values.

    I want smaller numbers.

    Take 0 -- it's a small number. If that isn't sufficient, define your
    requirements more precisely.

    We have functions like strtod and strtol
    to change chars into doubles or longs why not change time_t to int?
    gmtime() and localtime() do that.

    --
    Thad

    Comment

    • Keith Thompson

      #3
      Re: time()

      "Bill Cunningham" <nospam@nspam.c omwrites:
      I have no idea what to do with time () or another function to change the
      time_t data type to int to get numbers say from 0-20 that are ints.
      What for? If you want numbers in the range 0-20, what does that have
      to do with time()?
      time()
      reports a huge number of seconds and when using rand() truely produce a fake
      randomness.
      I don't know what you mean by that. You're using time() and rand()
      together somehow? How exactly? Presumably srand() is involved
      somehow, but you didn't mention it.
      I want smaller numbers. We have functions like strtod and strtol
      to change chars into doubles or longs why not change time_t to int?
      time_t is an arithmetic type capable of representing times. That's
      *all* the standard tells you about it. You can convert a time_t value
      to int the same way you'd convert any numeric type to another one
      (just assign it), but that's not a useful thing to do; for one thing,
      the result of time() may or may not fit within the range of int.

      Let's back off a bit. You're trying to accomplish something, but you
      haven't actually told us exactly what it is. Instead, you've thrown
      together a few things that you might *use* to accomplish something
      (time(), type time_t, type int, numbers in the range 0-20, rand(),
      "smaller numbers") -- things that apparently aren't doing what you
      want them to do.

      It's like asking "Should I use a hammer or a screwdriver? What about
      a staple gun?" without saying what you're trying to build.

      First, tell us *exactly* what you want to accomplish. State your
      problem without referring to any possible solution or piece of a
      solution.

      Then, *separately*, you might ask whether some proposed solution, or
      something like it, will do what you want. If you can express the
      proposed solution in C code, even if it doesn't work or is incorrect,
      that's great. But we can't help without knowing what you're trying to
      do.

      Have you read <http://www.catb.org/~esr/faqs/smart-questions.html> ?

      You might also take a look at section 13 in the comp.lang.c FAQ,
      particularly the questions on random numbers.

      --
      Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Bill Cunningham

        #4
        Re: time()

        [snip]
        We have functions like strtod and strtol
        >to change chars into doubles or longs why not change time_t to int?
        >
        gmtime() and localtime() do that.
        >
        OK Thad I'll try that. THanks.

        Bill


        Comment

        • Bill Cunningham

          #5
          Re: time()

          [snip]
          Let's back off a bit. You're trying to accomplish something, but you
          haven't actually told us exactly what it is. Instead, you've thrown
          together a few things that you might *use* to accomplish something
          (time(), type time_t, type int, numbers in the range 0-20, rand(),
          "smaller numbers") -- things that apparently aren't doing what you
          want them to do.
          >
          It's like asking "Should I use a hammer or a screwdriver? What about
          a staple gun?" without saying what you're trying to build.
          >
          First, tell us *exactly* what you want to accomplish. State your
          problem without referring to any possible solution or piece of a
          solution.
          >
          Then, *separately*, you might ask whether some proposed solution, or
          something like it, will do what you want. If you can express the
          proposed solution in C code, even if it doesn't work or is incorrect,
          that's great. But we can't help without knowing what you're trying to
          do.
          Say you have two dice of six sides. Can you use rand() to create 12
          options by rolling the die. Now there's another way you can do this simply
          but using arrays but I do not know how. Something like this.

          int a [12];

          Ok there's 12 objects or

          int a [20];

          like I want. I can use for to loop over them but I can't get randomness.
          That's a feature I do not know how to reproduce. It will have to someway
          depend on the system and most use the system time.

          Bill


          Comment

          • Gordon Burditt

            #6
            Re: time()

            I have no idea what to do with time () or another function to change the
            >time_t data type to int to get numbers say from 0-20 that are ints. time()
            What does the time have to do with the ints? Do you live on a
            planet where there are 20 hours in a day?

            If you want to convert the time to human-readable form (month, day,
            year, hour, minute, second) use localtime(), although none of those
            numbers run from 0-20 inclusive without also including some outside
            that range.
            >reports a huge number of seconds and when using rand() truely produce a fake
            >randomness. I want smaller numbers.
            If you are trying to use time() as a pseudo-random seed generator
            to get fake randomness, consider what the % operator does. What
            number range might time() % 21 produce? What might happen if you
            use it twice in the same second?
            >We have functions like strtod and strtol
            >to change chars into doubles or longs why not change time_t to int?
            time_t *is* an integer type, although it need not represent a "number
            of <time unitsince <epoch>". It might not fit in an int.

            Comment

            • Gordon Burditt

              #7
              Re: time()

              Say you have two dice of six sides. Can you use rand() to create 12
              >options by rolling the die.
              Note that rolling two dice of six sides each (adding the results)
              and rolling one die of 12 sides does NOT generate the same probability
              distribution.
              >Now there's another way you can do this simply
              >but using arrays but I do not know how. Something like this.
              >
              >int a [12];
              >
              >Ok there's 12 objects or
              >
              >int a [20];
              >
              >like I want. I can use for to loop over them but I can't get randomness.
              Ok, you can use an array of N elements to translate from a number from
              0 .. N-1 to something you want, where a[3] might be the code for the
              "+1 sword of troll slaying".

              Computers do not generate *real* random numbers without hardware
              support. Common ways of getting random numbers include measuring
              radioactive decay, measuring thermal noise, or fine-grained typing
              of manual events like keystrokes.
              >That's a feature I do not know how to reproduce. It will have to someway
              >depend on the system and most use the system time.
              Note that if you use pseudo-random numbers to implement casino games
              and let the public play these games, betting REAL money, you're going
              to lose your shirt.

              Something like rand() % 21 will generate numbers from 0 .. 20, inclusive,
              but the distribution will not be exactly even since 21 does not divide
              RAND_MAX+1 evenly.

              Seeding rand() is often done with the system time, like:

              srand((unsigned ) time() );

              Comment

              • Bill Cunningham

                #8
                Re: time()

                [snip]
                Note that if you use pseudo-random numbers to implement casino games
                and let the public play these games, betting REAL money, you're going
                to lose your shirt.
                >
                Something like rand() % 21 will generate numbers from 0 .. 20, inclusive,
                but the distribution will not be exactly even since 21 does not divide
                RAND_MAX+1 evenly.
                >
                Seeding rand() is often done with the system time, like:
                >
                srand((unsigned ) time() );
                I want to create a utility for playing online game like RPGs. So I was
                wanting to write a "dice rolling" utility. That's exactly what I am wanting.

                Bill


                Comment

                • Keith Thompson

                  #9
                  Re: time()

                  "Bill Cunningham" <nospam@nspam.c omwrites:
                  [snip]
                  >
                  >Let's back off a bit. You're trying to accomplish something, but you
                  >haven't actually told us exactly what it is. Instead, you've thrown
                  >together a few things that you might *use* to accomplish something
                  >(time(), type time_t, type int, numbers in the range 0-20, rand(),
                  >"smaller numbers") -- things that apparently aren't doing what you
                  >want them to do.
                  >>
                  >It's like asking "Should I use a hammer or a screwdriver? What about
                  >a staple gun?" without saying what you're trying to build.
                  >>
                  >First, tell us *exactly* what you want to accomplish. State your
                  >problem without referring to any possible solution or piece of a
                  >solution.
                  >>
                  >Then, *separately*, you might ask whether some proposed solution, or
                  >something like it, will do what you want. If you can express the
                  >proposed solution in C code, even if it doesn't work or is incorrect,
                  >that's great. But we can't help without knowing what you're trying to
                  >do.
                  I wrote the above. When you posted your followup, your newsreader
                  should have provided an attribution line, something like

                  Keith Thompson <kst-u@mib.orgwrites :

                  Please leave that line in place. Quoting somebody without attribution
                  is considered rude. (I think I've told you this before.)
                  Say you have two dice of six sides. Can you use rand() to create 12
                  options by rolling the die. Now there's another way you can do this simply
                  but using arrays but I do not know how. Something like this.
                  >
                  int a [12];
                  >
                  Ok there's 12 objects or
                  >
                  int a [20];
                  >
                  like I want. I can use for to loop over them but I can't get randomness.
                  That's a feature I do not know how to reproduce. It will have to someway
                  depend on the system and most use the system time.
                  You're still not making sense.

                  If I roll two six-sided dice, there are 36 (6*6) possible outcomes,
                  not 12. I suppose you could divide these up into 12 sets of 3
                  outcomes each, but I suspect that's not what you had in mind.

                  You stated your problem extremely vaguely, and mixed it with a
                  mishmash of possible pieces of a solution (rand(), a 12-element or
                  20-element array, a for loop, the system time). That's exactly what I
                  asked you not to do.

                  Please start by stating the problem you're trying to solve *without*
                  reference to the method used to solve it. Don't talk about rand(), or
                  time_t, or time(). Tell us what result you're trying to achieve. You
                  should be able to do that in one sentence or one short paragraph.

                  An example:

                  I want to generate a sequence of 73 uniformly distributed random
                  numbers, each in the range 37 to 153. I want the sequence to be
                  different each time I run my program.

                  (I've deliberately written something that differs from what you seem
                  to be looking for. You need to tell us what you want.)

                  Once you've done that, and *only* after you've done that, you can ask
                  whether certain tools (rand(), time(), a for loop) might or might not
                  be part of a solution.

                  --
                  Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Keith Thompson

                    #10
                    Re: time()

                    gordonb.3jhcg@b urditt.org (Gordon Burditt) writes:
                    [...]
                    time_t *is* an integer type, although it need not represent a "number
                    of <time unitsince <epoch>". It might not fit in an int.
                    time_t is an integer type on most implementations . The standard only
                    guarantees that it's an arithmetic type capable of representing times.
                    It could be floating-point. (It could even, in a sufficiently
                    perverse implementation, be complex or imaginary.)

                    (I do not give permission to quote this article, or any other article
                    I post to Usenet, without attribution.)

                    --
                    Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                    Nokia
                    "We must do something. This is something. Therefore, we must do this."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    • Bill Cunningham

                      #11
                      Re: time()


                      "Keith Thompson" <kst-u@mib.orgwrote in message
                      news:87lk2zj8ae .fsf@kvetch.smo v.org...
                      I wrote the above. When you posted your followup, your newsreader
                      should have provided an attribution line, something like
                      >
                      Keith Thompson <kst-u@mib.orgwrites :
                      >
                      Please leave that line in place. Quoting somebody without attribution
                      is considered rude. (I think I've told you this before.)
                      Now is this how it should be. I'm using outlook express :( It took me
                      awhile way way back to learn not to top post. Sorry I will work on this. I
                      have to do it all manually.

                      Bill


                      Comment

                      • Bill Cunningham

                        #12
                        Re: time()


                        "Keith Thompson" <kst-u@mib.orgwrote in message
                        news:87hcdnj7p4 .fsf@kvetch.smo v.org...
                        (I do not give permission to quote this article, or any other article
                        I post to Usenet, without attribution.)
                        >
                        What do you mean by this Keith? Do you not want to be quoted in clc's
                        message threads?

                        Bill


                        Comment

                        • Bartc

                          #13
                          Re: time()


                          "Keith Thompson" <kst-u@mib.orgwrote in message
                          news:87lk2zj8ae .fsf@kvetch.smo v.org...
                          "Bill Cunningham" <nospam@nspam.c omwrites:
                          >[snip]
                          > Say you have two dice of six sides. Can you use rand() to create 12
                          >options by rolling the die. Now there's another way you can do this
                          >simply
                          >but using arrays but I do not know how. Something like this.
                          >>
                          >int a [12];
                          >>
                          >Ok there's 12 objects or
                          >>
                          >int a [20];
                          >>
                          >like I want. I can use for to loop over them but I can't get randomness.
                          >That's a feature I do not know how to reproduce. It will have to someway
                          >depend on the system and most use the system time.
                          >
                          You're still not making sense.
                          >
                          If I roll two six-sided dice, there are 36 (6*6) possible outcomes,
                          not 12. I suppose you could divide these up into 12 sets of 3
                          outcomes each, but I suspect that's not what you had in mind.
                          I make it only 11 outcomes (1+1 to 6+6). Although if you listed all
                          combinations there would be 36.

                          If certain combinations are important, then you can list the 6 outcomes for
                          each die separately, totalling 12 outcomes.

                          --
                          Bartc



                          Comment

                          • pete

                            #14
                            Re: time()

                            Bill Cunningham wrote:
                            I want to create a utility for playing online game like RPGs. So I was
                            wanting to write a "dice rolling" utility. That's exactly what I am wanting.
                            /* BEGIN dice.c */

                            #include <stdio.h>
                            #include <stdlib.h>

                            #define THROWS 1000
                            #define str(s) # s
                            #define xstr(s) str(s)

                            int main(void)
                            {
                            int sum[13] = {0};
                            unsigned count = THROWS;

                            while (count-- != 0) {
                            ++sum[rand() % 6 + 1 + rand() % 6 + 1];
                            }
                            puts("Dice totals from "xstr(THROW S)" throws of a pair of dice:");
                            puts("value count");
                            for (count = 0; count != sizeof sum / sizeof *sum; ++count) {
                            printf("%2u %2d\n", count, sum[count]);
                            }
                            return 0;
                            }

                            /* END dice.c */

                            --
                            pete

                            Comment

                            • Bill Cunningham

                              #15
                              Re: time()


                              "pete" <pfiland@mindsp ring.comwrote in message
                              news:wsednTuJDI MHgIjVnZ2dnUVZ_ gSdnZ2d@earthli nk.com...
                              >
                              /* BEGIN dice.c */
                              >
                              #include <stdio.h>
                              #include <stdlib.h>
                              >
                              #define THROWS 1000
                              #define str(s) # s
                              #define xstr(s) str(s)
                              >
                              int main(void)
                              {
                              int sum[13] = {0};
                              unsigned count = THROWS;
                              >
                              while (count-- != 0) {
                              ++sum[rand() % 6 + 1 + rand() % 6 + 1];
                              }
                              puts("Dice totals from "xstr(THROW S)" throws of a pair of dice:");
                              puts("value count");
                              for (count = 0; count != sizeof sum / sizeof *sum; ++count) {
                              printf("%2u %2d\n", count, sum[count]);
                              }
                              return 0;
                              }
                              >
                              /* END dice.c */
                              >
                              Thanks Pete but I can't read some of the program. What does int sum
                              [13]={0}; mean? Why is there a zero in parenthesis? Also
                              #define str(s) # s
                              #define xstr(s) str(s)
                              What does that code do?

                              The rest I can pretty much make out. But what does the % after rand mean?

                              This is how to learn C.

                              Bill



                              Comment

                              Working...