Here is the code, but the outputs aren't coming out random? Maybe cause when the program runs it has the same time as all the loops?
#include <iostream>
using namespace std;
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
int main()
{
long int x = 0;
bool repeat = true;
srand( time(0));
int r1 = 2 + rand() % (11 - 2); //has a range of 2-10
int r3 = rand();
for (int i = 0; i <5; i++)
{
cout << r1 << endl; //loops 5 times but the numbers are all the same
cout << r3 << endl; // is it cause when the program is run all the times are
} // the same?
}
You need to move your calls to
rand()
to inside your loop:That said: since you're writing C++, you really want to use the new random number generation classes added in C++ 11 rather than using
srand
/rand
at all.You must call
rand()
each loop iteration.What was happening before was that you were calling
rand()
twice, one forr1
and once forr3
, and then simply printing the result 5 times.