Random Numbers

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Random Numbers

Postby Darobat » Sat Sep 27, 2003 2:53 pm

hi. just out of curiosity, how do you make a random number between 1 and 100. thanks.
User avatar
Darobat
Moderator
 
Posts: 2572
Joined: Sat Sep 27, 2003 1:19 pm

Re: Random Numbers

Postby raimo » Sat Sep 27, 2003 3:00 pm

Darobat wrote:hi. just out of curiosity, how do you make a random number between 1 and 100. thanks.

It's in the FAQ. Moreover, I do not know what "in the interval [y;x]" in the answer tries to say. Maybe there is a mistake? ;)
http://www.cpp-home.com/faq/faq.pl?3

Code: Select all
#include <ctime>
#include <cstdlib>
...
srand(time(NULL)); // set a seed number
...
int randombetween(int min, int max) {
/*
read the faq page for info about rand().
*/
    return rand()%(max-min+1)+min;
}
User avatar
raimo
 
Posts: 372
Joined: Fri Sep 26, 2003 6:50 am
Location: Finland


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 1 guest