0

I am using rand() to generate a random number and display it in the context of an arithmetic problem. I get the same two numbers everytime. How do I make these different each time?

#include <iostream>
#include<cstdlib>

using namespace std;

int main()
{

    int numberOne = rand()%1000;
    int numberTwo = rand()%1000;
    int numberThree = numberOne + numberTwo;
    char input;

    cout  << " " << numberOne << endl;
    cout <<  "+" << numberTwo << endl;
    cout << "----";
    cin.get();
    cout << numberThree << endl;
}
ashton
  • 57
  • 10

1 Answers1

1
#include <iostream>
#include<cstdlib>

using namespace std;

int main()
{

    /* initialize random seed: */
    srand (time(NULL));

    int numberOne = rand()%1000;
    int numberTwo = rand()%1000;
    int numberThree = numberOne + numberTwo;
    char input;

    cout  << " " << numberOne << endl;
    cout <<  "+" << numberTwo << endl;
    cout << "----";
    cin.get();
    cout << numberThree << endl;
}

In order for you to get a random number you need to provide a seed.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Jake Steele
  • 498
  • 3
  • 14