I want to generate random numbers that deppend on input from user.
For example if user inputs 8, I want to generate number from 5 to 7, (8 - 3) to (8 - 1)
My code:
#include <iostream>
#include <time.h>
int main()
{
int x1;
int x2;
std::srand(time(0));
while (true)
{
std::cout << "input : ";
std::cin >> x1;
x2 = std::rand() % (x1 - 1) + (x1 - 3);
std::cout << "result : " std::cout << x2 << std::endl;
}
return 0;
}
But the output is:
input : 8
result : 7
input : 8
result : 10
input : 8
result : 8
And I want result to be from 5 to 7.
Aucun commentaire:
Enregistrer un commentaire