March 19th, 2013, 02:48 AM
Rand() giving same number
The title says it all, rand() function from stdlib is giving me the same number every time. Even after restart.
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...
num1 = (rand() % 100);
So what would be the easiest solution to this?
March 19th, 2013, 07:21 AM
In linux u can do it by using process ID.
or if u using windows then try this in ur compiler:
March 19th, 2013, 07:32 AM
time.h is part of the standard library. You don't need to get it from anywhere.
Originally Posted by samans
March 19th, 2013, 10:19 AM
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.
March 19th, 2013, 11:32 AM
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 :)
March 20th, 2013, 12:11 AM
Im getting this error while compiling
Error  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?
March 20th, 2013, 10:25 AM
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
March 21st, 2013, 03:18 AM
#include "time.h" //looks for time.h in your app directory, which it wont find
Originally Posted by samans
#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.
March 21st, 2013, 10:29 AM
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.
Originally Posted by TechNoFear
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.
March 21st, 2013, 04:23 PM
Not quite. While it is compiler dependent, in many compilers, the following will happen:
Originally Posted by TechNoFear
#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