random number between 1 and 100 c++ code example
Example 1: c++ random number between 1 and 10
cout << (rand() % 10) + 1<<" ";
Example 2: random number generator c++ between 0 and 1
using namespace std;
int main()
{
srand(time(0));
cout<<"Random numbers generated between 1 and 10:"<<endl;
for(int i=0;i<10;i++)
cout << (rand() % 10) + 1<<" ";
return 0;
}
Example 3: making random numbers in c++
int main ()
{
int iSecret, iGuess;
srand (time(NULL));
iSecret = rand() % 10 + 1;
do {
printf ("Guess the number (1 to 10): ");
scanf ("%d",&iGuess);
if (iSecret<iGuess) puts ("The secret number is lower");
else if (iSecret>iGuess) puts ("The secret number is higher");
} while (iSecret!=iGuess);
puts ("Congratulations!");
return 0;
}