954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Random Number

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

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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()

int main()
{
   srand(time(0));
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thank you, it worked good. Now I actually get different "random" numbers when the program runs each time.

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You