#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Rand() giving same number


    The title says it all, rand() function from stdlib is giving me the same number every time. Even after restart.

    Code:
    	num1 = (rand() % 100);
    After intensive search on the net, I found I need to include time.h and use srand function to go about it. But I can't seem to get hold of this time.h header file from anywhere...

    So what would be the easiest solution to this?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    In linux u can do it by using process ID.

    or if u using windows then try this in ur compiler:

    Code:
    srand(time(NULL));
    rand();
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    Originally Posted by samans
    But I can't seem to get hold of this time.h header file from anywhere...
    time.h is part of the standard library. You don't need to get it from anywhere.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Additional note: you need to #include time.h just as you do stdio.h.

    If none of this has helped, then please specifically describe what part of using time.h you don't understand.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    Thanks! It worked now... When I included time.h earlier, it said it couldn't locate it.. I dunno why its found now.. :S Maybe just a typo error...

    Thanks for the help :)
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    Im getting this error while compiling

    Error [1027] unable to locate 'time.h'

    I had selected the wrong file for compilation last night. So it didnt give this error...

    Im using C18. And this time.h isn't inside it... How can I overcome this?
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    C18? Never heard of that compiler before, but Google tells me that's the name of an embedded system compiler. Is that what you're using? What is your target system?

    You never ever gave us any indication that you were using an embedded system compiler. Don't you think that maybe that would be something that we should know instead of just jerking us around?

    MicroChip has a forum at http://www.microchip.com/forums/f3.aspx. Since this is some specialty application with a specialty compiler, you would probably get the specialty answer to your specialty question sooner there than here.

    And when you do, please be more honest about what you're doing than you were here.

    Comments on this post

    • mythos_ agrees
  14. #8
  15. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    Originally Posted by samans
    Im getting this error while compiling

    Error [1027] unable to locate 'time.h'

    I had selected the wrong file for compilation last night. So it didnt give this error...

    Im using C18. And this time.h isn't inside it... How can I overcome this?
    #include "time.h" //looks for time.h in your app directory, which it wont find

    #include <time.h> //looks for time.h in the standard library directories, which it should find
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by TechNoFear
    #include "time.h" //looks for time.h in your app directory, which it wont find

    #include <time.h> //looks for time.h in the standard library directories, which it should find
    The problem here is that samans is trying to do this in an embedded system and failed to inform us of that simple yet highly pertinent fact. What should be in a standard implementation does not apply here.

    @samans:

    The reason why you were getting the same numbers is that rand uses a fairly deterministic formula to generate that sequence of pseudo-random numbers (please note the "pseudo-"). srand gives the pseudo-random number generator a value to start with. If rand starts with a particular number, it will always generate the same sequence of numbers; another particular number will produce a different sequence that is always the same. Therefore, to get a "random" sequence, you need to start rand with a uniquely different number each time.

    In UNIX systems (C and UNIX were co-developed together), time is measured by a count of seconds from midnight at the start of 1970 Jan 01. time() fetches that seconds count from the operating system and hence returns a unique value once per second (ie, if you call it twice within the same second, you will get the same value, so this uniqueness only applies if you don't call time() more than once during the same second). So the return value of time() is an ideal candidate for being the source of a unique value to pass to srand(). For that reason, every C programmer has learned to use it for this purpose, which is why all your research and our suggestions pointed to using time().

    However, that method will only work when there is operating-system support for time(). We are guaranteed that in standard implementations, since those are implemented in a computer which has an operating system (eg, PC, mini, main-frame). But unbeknownst to us you want to do this in an embedded system, which either does not have an operating system or else an RTOS that may or may not support time functions. And by the absense of time.h that you report, your embedded system does not support the standard time functions.

    This means that you need to use something else. Does your embedded system maintain a running count of seconds or milliseconds? Our older projects, which lack an actual OS, do keep track of the passage of time in that manner. If your system does have that kind of a counter, then read its current value and pass that to srand(). That would accomplish the same goal as using time() would under an actual operating system.
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Originally Posted by TechNoFear
    #include "time.h" //looks for time.h in your app directory, which it wont find

    #include <time.h> //looks for time.h in the standard library directories, which it should find
    Not quite. While it is compiler dependent, in many compilers, the following will happen:

    #include "time.h" // Looks for time.h in current directory first, next searches the user-specified include directories and then searches the standard include directories

    #include <time.h> // Looks for time.h in the standard include directories first, then searches the current directory and user-specified include directories

    By user-specified include directories, I mean the additional include directories that you can specify in the compiler options (for example, using the -I option for gcc compiler)

    In either case, the moment the compiler finds a time.h file in any of the searched directories, it will use whatever it finds. Hence, if you have multiple time.h files in different directories, it will use the first one that it finds.

    Note that the C98 standard says that the difference between #include "foo.h" vs. #include <foo.h> is "implementation-defined" (i.e. left to the compiler author), but many compilers implement it like what I described above (e.g. gcc, borland C++, turbo C, Visual C++ etc.)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo