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

Recommended Answers

All 2 Replies

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));
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.