What time.h library provide us?

This is a discussion on What time.h library provide us? within the C Programming forums, part of the General Programming Boards category; I use rand() and i don' t see the reason for using the <time.h> library...does anyone know?...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    What time.h library provide us?

    I use rand() and i don' t see the reason for using the <time.h> library...does anyone know?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Because one way to seed the pseudorandom number generator is to use the current time, or a hash thereof. Read Prelude's article on using rand().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    I know how to use rand, srand but i do not include <time.h> and still the output are radom numbers...why? i mean why then to use it if we already have what we want...i hope you understood what i am asking...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by brack
    I know how to use rand, srand but i do not include <time.h> and still the output are radom numbers...why?
    There are other ways to seed the pseudorandom number generator. If you do not explicitly seed it, it will be as if you called srand(1).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Oh, so that is why it returns random numbers again...thank you!

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    One more question, is there anyway that i can print the real time(that clock says while i run the program)?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    What exactly do you mean by that? You might want to start a new thread concerning this new problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brack View Post
    I know how to use rand, srand but i do not include <time.h> and still the output are radom numbers...why? i mean why then to use it if we already have what we want...i hope you understood what i am asking...
    I use srand(time) because time is never the same number twice... time marches on.

    If you use srand(12121) you will get the same sequence of numbers every time.

    Try it... you'll see....

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    i have done many programs like that and i don' t seem to agree...

    srand gives the seed number...so all the others are not the same...maybe i misunderstood what you said...

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brack View Post
    i have done many programs like that and i don' t seem to agree...

    srand gives the seed number...so all the others are not the same...maybe i misunderstood what you said...
    I'm sure this is compiler dependent, but in most of the C compilers I've used "srand(99999)" for example followed by a series of printf(rand() %10000) has always printed out exactly the same sequence every time.

    By using time as your seed you get a different seed and thus a different sequence, every second.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    before srand i was using scanf so every time there were a different number...never mind you are correct !

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    If you don't initialize the seed, or initialize it with the same number all the time, you will find that you will get the same sequence of numbers all the time!
    That's because a random number generator is not really so random. All it does is transform the seed using some algorithm into a new number. And then it uses that random number as the new seed to generate the next number, and so on. Since algorithms are deterministic, it will always produce the same output for any given number.
    Therefore, to get actual "random" numbers, you have to seed it with a unique seed everytime you run the program. The easiest way to do so is to use the current time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Library Program
    By the rofl waffle in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2010, 03:44 AM
  2. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  3. how to create an Import Library in Vs2008
    By khumayun in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2010, 10:49 AM
  4. link with C runtime library
    By George2 in forum C++ Programming
    Replies: 26
    Last Post: 02-05-2008, 12:56 AM
  5. Network Library
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2005, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21