# Thread: Help with random numbers and loops.

1. ## Help with random numbers and loops.

Hello everyone. I just got an assignment for my c++ class. I am almost done with it but im having hard time creating different random numbers. The assignment is "The program you will write is a History quiz. The following is a list of the first 10 US presidents in the order in which they were in office, along with the start and end years of their service. Write a program that will keep displaying the name and years in office for a president randomly selected from this list and ask the user to guess his chronological order. For example, if the program randomly selects and displays Thomas Jefferson (1801-1809), the right answer is 3 since he was the 3rd president. If the user enters the correct number, the program prints "Right!" and increments the correct answers count; otherwise, it prints "Wrong!" The program stops and displays the number of correct answers once 10 questions have been asked.If the president selected by the program is the same as the last one, have the program continue to randomly choose another until it's different form the last one.
We want to be lenient on the student, so for each question, allow 3 trials. If any of the 3 trials produces a correct answer, it should be counted as correct. Only if all 3 answers were wrong, show the right answer and don't give credit for the question. So, for each wrong answer, tell the user "Wrong!' and have him or her try again, for a total of 3 times. You must not repeat code, including for the 3 trials. "
but the program i wrote print the same president twice sometimes. I cant figure out a way to fix the bug. The code i wrote is below.

Code:
```#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main()
{
int number, last = 0, correct = 0, answer;

for (int i = 0; i < 10; i++)
{
do
{
srand((unsigned)time(0));
number = 1 + rand() % 10;
if (number == last)
number = 1 + rand() % 10;

else
{
if (number == 1)
cout << "George Washington (1789-1797) :";
else if (number == 2)
cout << "John Adams (1797-1801) :";
else if (number == 3)
cout << "Thomas Jefferson (1801-1809) :";
else if (number == 4)
cout << "James Madison (1808-1817) :";
else if (number == 5)
cout << "James Monroe 1817-1825) :";
else if (number == 6)
cout << "John Quincy Adams (1825-1829) :";
else if (number == 7)
cout << "Andrew Jackson (1829-1837) :";
else if (number == 8)
cout << "Martin Van Buren (1837-1841) :";
else if (number == 9)
cout << "William H. Harrison (1841) :";
else if (number == 10)
cout << "John Tyler (1841-1845) :";

break;
}
} while (last = number);
number = number + 1;

cout << "\n\nEnter the order number for each of the following presidents: ";

for (int j = 0; j < 4; j++)
{
{
cout << "\nRight!\n\n ";
correct++;
break;
}
else if (j == 3)
{
cout << "\nWrong! The correct answer is " << number << "\n\n";
break;
}
else
{
cout << "\nWrong! Try again :";
}
}
}
cout << "You answered " << correct << " of 10 correctly.\n";

cout << "\nPress any key to continue.";
_getch();
return 0;
}```

2. You should call srand() only once, preferably at the start of your program.