C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 19th, 2013, 02:48 AM
samans samans is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 15 samans User rank is Corporal (100 - 500 Reputation Level)samans User rank is Corporal (100 - 500 Reputation Level)samans User rank is Corporal (100 - 500 Reputation Level)samans User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 11 m 29 sec
Reputation 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?

Reply With Quote
  #2  
Old March 19th, 2013, 07:21 AM
eramit2010 eramit2010 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Location: India
Posts: 23 eramit2010 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 24 m 49 sec
Reputation Power: 0
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();

Reply With Quote
  #3  
Old March 19th, 2013, 07:32 AM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 20 m 52 sec
Reputation Power: 38
Quote:
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.

Reply With Quote
  #4  
Old March 19th, 2013, 10:19 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 5 m 8 sec
Reputation Power: 1949
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.

Reply With Quote
  #5  
Old March 19th, 2013, 11:32 AM
samans samans is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 15 samans User rank is Corporal (100 - 500 Reputation Level)samans User rank is Corporal (100 - 500 Reputation Level)samans User rank is Corporal (100 - 500 Reputation Level)samans User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 11 m 29 sec
Reputation 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

Reply With Quote
  #6  
Old March 20th, 2013, 12:11 AM
samans samans is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 15 samans User rank is Corporal (100 - 500 Reputation Level)samans User rank is Corporal (100 - 500 Reputation Level)samans User rank is Corporal (100 - 500 Reputation Level)samans User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 h 11 m 29 sec
Reputation 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?

Reply With Quote
  #7  
Old March 20th, 2013, 10:25 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 5 m 8 sec
Reputation Power: 1949
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!

Reply With Quote
  #8  
Old March 21st, 2013, 03:18 AM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 618 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 13 h 50 m
Reputation Power: 26
Quote:
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

Reply With Quote
  #9  
Old March 21st, 2013, 10:29 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 5 m 8 sec
Reputation Power: 1949
Quote:
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.

Reply With Quote
  #10  
Old March 21st, 2013, 04:23 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 31 m 48 sec
Reputation Power: 4080
Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Rand() giving same number

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap