finalists

Program: You have four identical prizes to give away and a pool of 25 finalists. The finalists are assigned numbers from 1 to 25. Write a program to randomly select the numbers of 4 finalists to receive a prize. Make sure no to pick the same number twice. For example, picking finalists 3,15,22 and 14 would be valid but picking 3,3,31 and 17 would be invalid because finalists number 3 is listed twice and 31 is not a valid number.

what i have so far:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand(time(0));
int i;
for(i=1;i<=4;i++){
cout<<(rand()%25+1)<<endl;
}
if ((rand()%25+1)==(rand()%25+1)){
for(i=1;i<=4;i++){
cout<<(rand()%25+1)<<endl;
}
}
return 0;
}

Question:
For some reason i keep on getting an out that has the same number in it like this 19,12, 1 and 19. In which i can not have 19 twice. I don't understand why it is doing this at all please help
Every time you call rand() you get a different number.

So this check here:
if ((rand()%25+1)==(rand()%25+1)){

Does absolutely nothing, because the random numbers you are comparing here are going to be completely different from the random number you print below.

You will need to keep track of the numbers you choose by putting them in a variable.
Last edited on
Topic archived. No new replies allowed.