how to generate random number between 0 to 100 in vc++?
Printable View
how to generate random number between 0 to 100 in vc++?
#include <stdlib.h> //for rand func
#include <time.h> //time func used in seeding
int main()
{
srand(time(NULL)); //seeds the number
int mynum = rand()%100+1; //gets the random num
cout<<mynum<<endl; //outputs it
return 0;
}
:)
If you're going to post C++, you need to have::)Code:#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
srand(time(NULL)); //seeds the number
int mynum = rand() % 100 + 1; //gets the random num
cout << mynum << endl; //outputs it
}
And this creates a random number from 1 to 100 (both inclusive), if you want to have from 0 to 100 (both inclusive) you need
rand() % 101
Thanks very much