Problem with random numbers repeating

closed account (E3h7X9L8)
Program who guesses your number(and counts guesses) but I want the program not to repeat random numbers when he is asking you .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{   int n;
    cout << "Write your number ";cin>> n;
    srand(time(0));
    int rand();
    int guess;
    guess=(rand() % (n*2)) +1;
    int j=1;
    char answer;
    do
    {
        cout<<"Is your number "<<guess<<" ?(y/n)"<<endl;
        cin>>answer;
        if (answer=='y') break;
        else
        guess=(rand() % (n*2)) +1;
        ++j;

    }
    while(n);
 if (guess==n) {cout<<"\n\nYour number is "<<guess<<endl;
               cout<<"I did "<<j<<" guesses"<<endl;}


    return 0;
}
Hi leryss,

If you don't want repetition you have to store your previous guesses.
Have you already worked with arrays?

Basically you have surround your guess=(rand()...) with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define MAX_NUMBER_OF_MEMORIZED_GUESSES 50 // "MAX_NUMBER_OF_MEMORIZED_GUESSES" will be replaced with the number 50 everywhere in the code
bool newGuess=true; //this variable will tell us at the end of the loop, if our random number was never taken before
int prevguesses[MAX_NUMBER_OF_MEMORIZED_GUESSES]; // this is like a list of integers. google array's to learn how they work if you don't know (or ask me)
int numOfGuesses=0;
for(int i=0;i<MAX_NUMBER_OF_MEMORIZED_GUESSES;i++) // here we set all values to 0 (if 0 is a valid value in your game you have to set it to something else e.g -1)
  prevguesses[i]=0;

do
{
  newGuess=true;
  guess=(rand()...); //generating new random-number
  for(int i=0;i<numOfGuesses || newGuess==true;i++) // walking through the array until we are at the end or we see that our random-number is already in our list
  {
    if(guesses[i] == guess) // here we take memorized guess number i and compare it to our generated one
    {
      newGuess=false; // if they are the same we stop the for loop, come to the end of the do-while, and start all over from do
    }
  }
}while(newGuess==false); // in the other case we leave and have a completely new and not repeated number
 // now we have to store our new guess, so it can be compared next time of guessing (if the computer made the wrong guess)
guesses[numOfGuesses]=guess; //so we take the highest position in the array and make it our new value
numOfGuesses++; //now we increase the number of guesses we have stored, so we know how long our checking for-loop will run and where to store our next memorized element.

---------------
I should add that your program will cause problems when you get more than MAX_NUMBER_OF_MEMORIZED_GUESSES.
To solve this the easiest option is to surround your code with something like:
1
2
3
4
5
6
7
8
9
if (numOfGuesses < MAX_NUMBER_OF_MEMORIZED_GUESSES)
{
//here the code
}
else
{
  cout << "I don't know, you win.";
  return 0;
}


I konw, this is a lot, but long story short: store all your guesses and check this list, and if you already had this one, get another value to guess.

nonsence90.
Topic archived. No new replies allowed.