hi guys!
i need to generate a random number which must be between 3 and 10 and it must be an even number;
is there any code you help me with?
thanks by the way.
i am a begineer programmer,i cant understand.
if i get a situation like i must ramdomly choose odd numbers between 0 and 32767;
is there any easy method
#include <iostream>
#include <cstdlib>
#include <ctime>
int RandomEvenNumber(int min, int max)
{
int num = -1; // some initial value
do
{
num = min + (rand() % ((max - min) + 1));
}
while(num % 2 != 0);
return num;
}
int main()
{
constint MIN = 3;
constint MAX = 10;
srand (time(0));
for (int i = 0; i < 20; i++)
{
int num = RandomEvenNumber(MIN, MAX);
std::cout << "Num = " << num << "\n";
}
system("pause");
return 0;
}
Instead of putting a blanket usingnamespace std; at the top of the program, you may choose to qualify functions individually with the name of the namespace, whether it's std:: or someothernamespace::