944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1214
  • C++ RSS
Apr 18th, 2007
0

Random Number

Expand Post »
Hey, I was just wondering how to get a variable assigned a random number within a certain range. I tried the rand, but I seem to get the same set of numbers everytime I run the code...

I did some research on random numbers in C++ and understand the whole pourpose of seeding random generation, but couldn't find anywhere that just gives me an example on how to do it. Does anyone know a website that could show me what to do? Or even show me here?

Thanks everyone,
-Matt
Similar Threads
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Apr 18th, 2007
0

Re: Random Number

srand() seeds the random number generator, you should call srand() one time only and near the top of main(). Most of us use the time() function as the paramter to srand()
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. srand(time(0));
  4. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,959 posts
since Aug 2005
Apr 18th, 2007
0

Re: Random Number

Thank you, it worked good. Now I actually get different "random" numbers when the program runs each time.
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: LNK2001 error
Next Thread in C++ Forum Timeline: Question???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC